mce.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498
  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. int i, ret = 0;
  644. char *tmp;
  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. __set_bit(i, validp);
  649. if (quirk_no_way_out)
  650. quirk_no_way_out(i, m, regs);
  651. }
  652. if (mce_severity(m, mca_cfg.tolerant, &tmp, true) >= MCE_PANIC_SEVERITY) {
  653. *msg = tmp;
  654. ret = 1;
  655. }
  656. }
  657. return ret;
  658. }
  659. /*
  660. * Variable to establish order between CPUs while scanning.
  661. * Each CPU spins initially until executing is equal its number.
  662. */
  663. static atomic_t mce_executing;
  664. /*
  665. * Defines order of CPUs on entry. First CPU becomes Monarch.
  666. */
  667. static atomic_t mce_callin;
  668. /*
  669. * Check if a timeout waiting for other CPUs happened.
  670. */
  671. static int mce_timed_out(u64 *t, const char *msg)
  672. {
  673. /*
  674. * The others already did panic for some reason.
  675. * Bail out like in a timeout.
  676. * rmb() to tell the compiler that system_state
  677. * might have been modified by someone else.
  678. */
  679. rmb();
  680. if (atomic_read(&mce_panicked))
  681. wait_for_panic();
  682. if (!mca_cfg.monarch_timeout)
  683. goto out;
  684. if ((s64)*t < SPINUNIT) {
  685. if (mca_cfg.tolerant <= 1)
  686. mce_panic(msg, NULL, NULL);
  687. cpu_missing = 1;
  688. return 1;
  689. }
  690. *t -= SPINUNIT;
  691. out:
  692. touch_nmi_watchdog();
  693. return 0;
  694. }
  695. /*
  696. * The Monarch's reign. The Monarch is the CPU who entered
  697. * the machine check handler first. It waits for the others to
  698. * raise the exception too and then grades them. When any
  699. * error is fatal panic. Only then let the others continue.
  700. *
  701. * The other CPUs entering the MCE handler will be controlled by the
  702. * Monarch. They are called Subjects.
  703. *
  704. * This way we prevent any potential data corruption in a unrecoverable case
  705. * and also makes sure always all CPU's errors are examined.
  706. *
  707. * Also this detects the case of a machine check event coming from outer
  708. * space (not detected by any CPUs) In this case some external agent wants
  709. * us to shut down, so panic too.
  710. *
  711. * The other CPUs might still decide to panic if the handler happens
  712. * in a unrecoverable place, but in this case the system is in a semi-stable
  713. * state and won't corrupt anything by itself. It's ok to let the others
  714. * continue for a bit first.
  715. *
  716. * All the spin loops have timeouts; when a timeout happens a CPU
  717. * typically elects itself to be Monarch.
  718. */
  719. static void mce_reign(void)
  720. {
  721. int cpu;
  722. struct mce *m = NULL;
  723. int global_worst = 0;
  724. char *msg = NULL;
  725. char *nmsg = NULL;
  726. /*
  727. * This CPU is the Monarch and the other CPUs have run
  728. * through their handlers.
  729. * Grade the severity of the errors of all the CPUs.
  730. */
  731. for_each_possible_cpu(cpu) {
  732. int severity = mce_severity(&per_cpu(mces_seen, cpu),
  733. mca_cfg.tolerant,
  734. &nmsg, true);
  735. if (severity > global_worst) {
  736. msg = nmsg;
  737. global_worst = severity;
  738. m = &per_cpu(mces_seen, cpu);
  739. }
  740. }
  741. /*
  742. * Cannot recover? Panic here then.
  743. * This dumps all the mces in the log buffer and stops the
  744. * other CPUs.
  745. */
  746. if (m && global_worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3)
  747. mce_panic("Fatal machine check", m, msg);
  748. /*
  749. * For UC somewhere we let the CPU who detects it handle it.
  750. * Also must let continue the others, otherwise the handling
  751. * CPU could deadlock on a lock.
  752. */
  753. /*
  754. * No machine check event found. Must be some external
  755. * source or one CPU is hung. Panic.
  756. */
  757. if (global_worst <= MCE_KEEP_SEVERITY && mca_cfg.tolerant < 3)
  758. mce_panic("Fatal machine check from unknown source", NULL, NULL);
  759. /*
  760. * Now clear all the mces_seen so that they don't reappear on
  761. * the next mce.
  762. */
  763. for_each_possible_cpu(cpu)
  764. memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
  765. }
  766. static atomic_t global_nwo;
  767. /*
  768. * Start of Monarch synchronization. This waits until all CPUs have
  769. * entered the exception handler and then determines if any of them
  770. * saw a fatal event that requires panic. Then it executes them
  771. * in the entry order.
  772. * TBD double check parallel CPU hotunplug
  773. */
  774. static int mce_start(int *no_way_out)
  775. {
  776. int order;
  777. int cpus = num_online_cpus();
  778. u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
  779. if (!timeout)
  780. return -1;
  781. atomic_add(*no_way_out, &global_nwo);
  782. /*
  783. * Rely on the implied barrier below, such that global_nwo
  784. * is updated before mce_callin.
  785. */
  786. order = atomic_inc_return(&mce_callin);
  787. /*
  788. * Wait for everyone.
  789. */
  790. while (atomic_read(&mce_callin) != cpus) {
  791. if (mce_timed_out(&timeout,
  792. "Timeout: Not all CPUs entered broadcast exception handler")) {
  793. atomic_set(&global_nwo, 0);
  794. return -1;
  795. }
  796. ndelay(SPINUNIT);
  797. }
  798. /*
  799. * mce_callin should be read before global_nwo
  800. */
  801. smp_rmb();
  802. if (order == 1) {
  803. /*
  804. * Monarch: Starts executing now, the others wait.
  805. */
  806. atomic_set(&mce_executing, 1);
  807. } else {
  808. /*
  809. * Subject: Now start the scanning loop one by one in
  810. * the original callin order.
  811. * This way when there are any shared banks it will be
  812. * only seen by one CPU before cleared, avoiding duplicates.
  813. */
  814. while (atomic_read(&mce_executing) < order) {
  815. if (mce_timed_out(&timeout,
  816. "Timeout: Subject CPUs unable to finish machine check processing")) {
  817. atomic_set(&global_nwo, 0);
  818. return -1;
  819. }
  820. ndelay(SPINUNIT);
  821. }
  822. }
  823. /*
  824. * Cache the global no_way_out state.
  825. */
  826. *no_way_out = atomic_read(&global_nwo);
  827. return order;
  828. }
  829. /*
  830. * Synchronize between CPUs after main scanning loop.
  831. * This invokes the bulk of the Monarch processing.
  832. */
  833. static int mce_end(int order)
  834. {
  835. int ret = -1;
  836. u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
  837. if (!timeout)
  838. goto reset;
  839. if (order < 0)
  840. goto reset;
  841. /*
  842. * Allow others to run.
  843. */
  844. atomic_inc(&mce_executing);
  845. if (order == 1) {
  846. /* CHECKME: Can this race with a parallel hotplug? */
  847. int cpus = num_online_cpus();
  848. /*
  849. * Monarch: Wait for everyone to go through their scanning
  850. * loops.
  851. */
  852. while (atomic_read(&mce_executing) <= cpus) {
  853. if (mce_timed_out(&timeout,
  854. "Timeout: Monarch CPU unable to finish machine check processing"))
  855. goto reset;
  856. ndelay(SPINUNIT);
  857. }
  858. mce_reign();
  859. barrier();
  860. ret = 0;
  861. } else {
  862. /*
  863. * Subject: Wait for Monarch to finish.
  864. */
  865. while (atomic_read(&mce_executing) != 0) {
  866. if (mce_timed_out(&timeout,
  867. "Timeout: Monarch CPU did not finish machine check processing"))
  868. goto reset;
  869. ndelay(SPINUNIT);
  870. }
  871. /*
  872. * Don't reset anything. That's done by the Monarch.
  873. */
  874. return 0;
  875. }
  876. /*
  877. * Reset all global state.
  878. */
  879. reset:
  880. atomic_set(&global_nwo, 0);
  881. atomic_set(&mce_callin, 0);
  882. barrier();
  883. /*
  884. * Let others run again.
  885. */
  886. atomic_set(&mce_executing, 0);
  887. return ret;
  888. }
  889. static void mce_clear_state(unsigned long *toclear)
  890. {
  891. int i;
  892. for (i = 0; i < mca_cfg.banks; i++) {
  893. if (test_bit(i, toclear))
  894. mce_wrmsrl(msr_ops.status(i), 0);
  895. }
  896. }
  897. static int do_memory_failure(struct mce *m)
  898. {
  899. int flags = MF_ACTION_REQUIRED;
  900. int ret;
  901. pr_err("Uncorrected hardware memory error in user-access at %llx", m->addr);
  902. if (!(m->mcgstatus & MCG_STATUS_RIPV))
  903. flags |= MF_MUST_KILL;
  904. ret = memory_failure(m->addr >> PAGE_SHIFT, flags);
  905. if (ret)
  906. pr_err("Memory error not recovered");
  907. else
  908. mce_unmap_kpfn(m->addr >> PAGE_SHIFT);
  909. return ret;
  910. }
  911. #ifndef mce_unmap_kpfn
  912. static void mce_unmap_kpfn(unsigned long pfn)
  913. {
  914. unsigned long decoy_addr;
  915. /*
  916. * Unmap this page from the kernel 1:1 mappings to make sure
  917. * we don't log more errors because of speculative access to
  918. * the page.
  919. * We would like to just call:
  920. * set_memory_np((unsigned long)pfn_to_kaddr(pfn), 1);
  921. * but doing that would radically increase the odds of a
  922. * speculative access to the poison page because we'd have
  923. * the virtual address of the kernel 1:1 mapping sitting
  924. * around in registers.
  925. * Instead we get tricky. We create a non-canonical address
  926. * that looks just like the one we want, but has bit 63 flipped.
  927. * This relies on set_memory_np() not checking whether we passed
  928. * a legal address.
  929. */
  930. decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63));
  931. if (set_memory_np(decoy_addr, 1))
  932. pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn);
  933. }
  934. #endif
  935. /*
  936. * The actual machine check handler. This only handles real
  937. * exceptions when something got corrupted coming in through int 18.
  938. *
  939. * This is executed in NMI context not subject to normal locking rules. This
  940. * implies that most kernel services cannot be safely used. Don't even
  941. * think about putting a printk in there!
  942. *
  943. * On Intel systems this is entered on all CPUs in parallel through
  944. * MCE broadcast. However some CPUs might be broken beyond repair,
  945. * so be always careful when synchronizing with others.
  946. */
  947. void do_machine_check(struct pt_regs *regs, long error_code)
  948. {
  949. struct mca_config *cfg = &mca_cfg;
  950. struct mce m, *final;
  951. int i;
  952. int worst = 0;
  953. int severity;
  954. /*
  955. * Establish sequential order between the CPUs entering the machine
  956. * check handler.
  957. */
  958. int order = -1;
  959. /*
  960. * If no_way_out gets set, there is no safe way to recover from this
  961. * MCE. If mca_cfg.tolerant is cranked up, we'll try anyway.
  962. */
  963. int no_way_out = 0;
  964. /*
  965. * If kill_it gets set, there might be a way to recover from this
  966. * error.
  967. */
  968. int kill_it = 0;
  969. DECLARE_BITMAP(toclear, MAX_NR_BANKS);
  970. DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
  971. char *msg = "Unknown";
  972. /*
  973. * MCEs are always local on AMD. Same is determined by MCG_STATUS_LMCES
  974. * on Intel.
  975. */
  976. int lmce = 1;
  977. int cpu = smp_processor_id();
  978. /*
  979. * Cases where we avoid rendezvous handler timeout:
  980. * 1) If this CPU is offline.
  981. *
  982. * 2) If crashing_cpu was set, e.g. we're entering kdump and we need to
  983. * skip those CPUs which remain looping in the 1st kernel - see
  984. * crash_nmi_callback().
  985. *
  986. * Note: there still is a small window between kexec-ing and the new,
  987. * kdump kernel establishing a new #MC handler where a broadcasted MCE
  988. * might not get handled properly.
  989. */
  990. if (cpu_is_offline(cpu) ||
  991. (crashing_cpu != -1 && crashing_cpu != cpu)) {
  992. u64 mcgstatus;
  993. mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  994. if (mcgstatus & MCG_STATUS_RIPV) {
  995. mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
  996. return;
  997. }
  998. }
  999. ist_enter(regs);
  1000. this_cpu_inc(mce_exception_count);
  1001. if (!cfg->banks)
  1002. goto out;
  1003. mce_gather_info(&m, regs);
  1004. m.tsc = rdtsc();
  1005. final = this_cpu_ptr(&mces_seen);
  1006. *final = m;
  1007. memset(valid_banks, 0, sizeof(valid_banks));
  1008. no_way_out = mce_no_way_out(&m, &msg, valid_banks, regs);
  1009. barrier();
  1010. /*
  1011. * When no restart IP might need to kill or panic.
  1012. * Assume the worst for now, but if we find the
  1013. * severity is MCE_AR_SEVERITY we have other options.
  1014. */
  1015. if (!(m.mcgstatus & MCG_STATUS_RIPV))
  1016. kill_it = 1;
  1017. /*
  1018. * Check if this MCE is signaled to only this logical processor,
  1019. * on Intel only.
  1020. */
  1021. if (m.cpuvendor == X86_VENDOR_INTEL)
  1022. lmce = m.mcgstatus & MCG_STATUS_LMCES;
  1023. /*
  1024. * Go through all banks in exclusion of the other CPUs. This way we
  1025. * don't report duplicated events on shared banks because the first one
  1026. * to see it will clear it. If this is a Local MCE, then no need to
  1027. * perform rendezvous.
  1028. */
  1029. if (!lmce)
  1030. order = mce_start(&no_way_out);
  1031. for (i = 0; i < cfg->banks; i++) {
  1032. __clear_bit(i, toclear);
  1033. if (!test_bit(i, valid_banks))
  1034. continue;
  1035. if (!mce_banks[i].ctl)
  1036. continue;
  1037. m.misc = 0;
  1038. m.addr = 0;
  1039. m.bank = i;
  1040. m.status = mce_rdmsrl(msr_ops.status(i));
  1041. if ((m.status & MCI_STATUS_VAL) == 0)
  1042. continue;
  1043. /*
  1044. * Non uncorrected or non signaled errors are handled by
  1045. * machine_check_poll. Leave them alone, unless this panics.
  1046. */
  1047. if (!(m.status & (cfg->ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
  1048. !no_way_out)
  1049. continue;
  1050. /*
  1051. * Set taint even when machine check was not enabled.
  1052. */
  1053. add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
  1054. severity = mce_severity(&m, cfg->tolerant, NULL, true);
  1055. /*
  1056. * When machine check was for corrected/deferred handler don't
  1057. * touch, unless we're panicing.
  1058. */
  1059. if ((severity == MCE_KEEP_SEVERITY ||
  1060. severity == MCE_UCNA_SEVERITY) && !no_way_out)
  1061. continue;
  1062. __set_bit(i, toclear);
  1063. if (severity == MCE_NO_SEVERITY) {
  1064. /*
  1065. * Machine check event was not enabled. Clear, but
  1066. * ignore.
  1067. */
  1068. continue;
  1069. }
  1070. mce_read_aux(&m, i);
  1071. /* assuming valid severity level != 0 */
  1072. m.severity = severity;
  1073. mce_log(&m);
  1074. if (severity > worst) {
  1075. *final = m;
  1076. worst = severity;
  1077. }
  1078. }
  1079. /* mce_clear_state will clear *final, save locally for use later */
  1080. m = *final;
  1081. if (!no_way_out)
  1082. mce_clear_state(toclear);
  1083. /*
  1084. * Do most of the synchronization with other CPUs.
  1085. * When there's any problem use only local no_way_out state.
  1086. */
  1087. if (!lmce) {
  1088. if (mce_end(order) < 0)
  1089. no_way_out = worst >= MCE_PANIC_SEVERITY;
  1090. } else {
  1091. /*
  1092. * Local MCE skipped calling mce_reign()
  1093. * If we found a fatal error, we need to panic here.
  1094. */
  1095. if (worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3)
  1096. mce_panic("Machine check from unknown source",
  1097. NULL, NULL);
  1098. }
  1099. /*
  1100. * If tolerant is at an insane level we drop requests to kill
  1101. * processes and continue even when there is no way out.
  1102. */
  1103. if (cfg->tolerant == 3)
  1104. kill_it = 0;
  1105. else if (no_way_out)
  1106. mce_panic("Fatal machine check on current CPU", &m, msg);
  1107. if (worst > 0)
  1108. mce_report_event(regs);
  1109. mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
  1110. out:
  1111. sync_core();
  1112. if (worst != MCE_AR_SEVERITY && !kill_it)
  1113. goto out_ist;
  1114. /* Fault was in user mode and we need to take some action */
  1115. if ((m.cs & 3) == 3) {
  1116. ist_begin_non_atomic(regs);
  1117. local_irq_enable();
  1118. if (kill_it || do_memory_failure(&m))
  1119. force_sig(SIGBUS, current);
  1120. local_irq_disable();
  1121. ist_end_non_atomic();
  1122. } else {
  1123. if (!fixup_exception(regs, X86_TRAP_MC))
  1124. mce_panic("Failed kernel mode recovery", &m, NULL);
  1125. }
  1126. out_ist:
  1127. ist_exit(regs);
  1128. }
  1129. EXPORT_SYMBOL_GPL(do_machine_check);
  1130. #ifndef CONFIG_MEMORY_FAILURE
  1131. int memory_failure(unsigned long pfn, int flags)
  1132. {
  1133. /* mce_severity() should not hand us an ACTION_REQUIRED error */
  1134. BUG_ON(flags & MF_ACTION_REQUIRED);
  1135. pr_err("Uncorrected memory error in page 0x%lx ignored\n"
  1136. "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n",
  1137. pfn);
  1138. return 0;
  1139. }
  1140. #endif
  1141. /*
  1142. * Periodic polling timer for "silent" machine check errors. If the
  1143. * poller finds an MCE, poll 2x faster. When the poller finds no more
  1144. * errors, poll 2x slower (up to check_interval seconds).
  1145. */
  1146. static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
  1147. static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
  1148. static DEFINE_PER_CPU(struct timer_list, mce_timer);
  1149. static unsigned long mce_adjust_timer_default(unsigned long interval)
  1150. {
  1151. return interval;
  1152. }
  1153. static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
  1154. static void __start_timer(struct timer_list *t, unsigned long interval)
  1155. {
  1156. unsigned long when = jiffies + interval;
  1157. unsigned long flags;
  1158. local_irq_save(flags);
  1159. if (!timer_pending(t) || time_before(when, t->expires))
  1160. mod_timer(t, round_jiffies(when));
  1161. local_irq_restore(flags);
  1162. }
  1163. static void mce_timer_fn(struct timer_list *t)
  1164. {
  1165. struct timer_list *cpu_t = this_cpu_ptr(&mce_timer);
  1166. unsigned long iv;
  1167. WARN_ON(cpu_t != t);
  1168. iv = __this_cpu_read(mce_next_interval);
  1169. if (mce_available(this_cpu_ptr(&cpu_info))) {
  1170. machine_check_poll(0, this_cpu_ptr(&mce_poll_banks));
  1171. if (mce_intel_cmci_poll()) {
  1172. iv = mce_adjust_timer(iv);
  1173. goto done;
  1174. }
  1175. }
  1176. /*
  1177. * Alert userspace if needed. If we logged an MCE, reduce the polling
  1178. * interval, otherwise increase the polling interval.
  1179. */
  1180. if (mce_notify_irq())
  1181. iv = max(iv / 2, (unsigned long) HZ/100);
  1182. else
  1183. iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
  1184. done:
  1185. __this_cpu_write(mce_next_interval, iv);
  1186. __start_timer(t, iv);
  1187. }
  1188. /*
  1189. * Ensure that the timer is firing in @interval from now.
  1190. */
  1191. void mce_timer_kick(unsigned long interval)
  1192. {
  1193. struct timer_list *t = this_cpu_ptr(&mce_timer);
  1194. unsigned long iv = __this_cpu_read(mce_next_interval);
  1195. __start_timer(t, interval);
  1196. if (interval < iv)
  1197. __this_cpu_write(mce_next_interval, interval);
  1198. }
  1199. /* Must not be called in IRQ context where del_timer_sync() can deadlock */
  1200. static void mce_timer_delete_all(void)
  1201. {
  1202. int cpu;
  1203. for_each_online_cpu(cpu)
  1204. del_timer_sync(&per_cpu(mce_timer, cpu));
  1205. }
  1206. /*
  1207. * Notify the user(s) about new machine check events.
  1208. * Can be called from interrupt context, but not from machine check/NMI
  1209. * context.
  1210. */
  1211. int mce_notify_irq(void)
  1212. {
  1213. /* Not more than two messages every minute */
  1214. static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  1215. if (test_and_clear_bit(0, &mce_need_notify)) {
  1216. mce_work_trigger();
  1217. if (__ratelimit(&ratelimit))
  1218. pr_info(HW_ERR "Machine check events logged\n");
  1219. return 1;
  1220. }
  1221. return 0;
  1222. }
  1223. EXPORT_SYMBOL_GPL(mce_notify_irq);
  1224. static int __mcheck_cpu_mce_banks_init(void)
  1225. {
  1226. int i;
  1227. u8 num_banks = mca_cfg.banks;
  1228. mce_banks = kcalloc(num_banks, sizeof(struct mce_bank), GFP_KERNEL);
  1229. if (!mce_banks)
  1230. return -ENOMEM;
  1231. for (i = 0; i < num_banks; i++) {
  1232. struct mce_bank *b = &mce_banks[i];
  1233. b->ctl = -1ULL;
  1234. b->init = 1;
  1235. }
  1236. return 0;
  1237. }
  1238. /*
  1239. * Initialize Machine Checks for a CPU.
  1240. */
  1241. static int __mcheck_cpu_cap_init(void)
  1242. {
  1243. unsigned b;
  1244. u64 cap;
  1245. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1246. b = cap & MCG_BANKCNT_MASK;
  1247. if (!mca_cfg.banks)
  1248. pr_info("CPU supports %d MCE banks\n", b);
  1249. if (b > MAX_NR_BANKS) {
  1250. pr_warn("Using only %u machine check banks out of %u\n",
  1251. MAX_NR_BANKS, b);
  1252. b = MAX_NR_BANKS;
  1253. }
  1254. /* Don't support asymmetric configurations today */
  1255. WARN_ON(mca_cfg.banks != 0 && b != mca_cfg.banks);
  1256. mca_cfg.banks = b;
  1257. if (!mce_banks) {
  1258. int err = __mcheck_cpu_mce_banks_init();
  1259. if (err)
  1260. return err;
  1261. }
  1262. /* Use accurate RIP reporting if available. */
  1263. if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
  1264. mca_cfg.rip_msr = MSR_IA32_MCG_EIP;
  1265. if (cap & MCG_SER_P)
  1266. mca_cfg.ser = 1;
  1267. return 0;
  1268. }
  1269. static void __mcheck_cpu_init_generic(void)
  1270. {
  1271. enum mcp_flags m_fl = 0;
  1272. mce_banks_t all_banks;
  1273. u64 cap;
  1274. if (!mca_cfg.bootlog)
  1275. m_fl = MCP_DONTLOG;
  1276. /*
  1277. * Log the machine checks left over from the previous reset.
  1278. */
  1279. bitmap_fill(all_banks, MAX_NR_BANKS);
  1280. machine_check_poll(MCP_UC | m_fl, &all_banks);
  1281. cr4_set_bits(X86_CR4_MCE);
  1282. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1283. if (cap & MCG_CTL_P)
  1284. wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
  1285. }
  1286. static void __mcheck_cpu_init_clear_banks(void)
  1287. {
  1288. int i;
  1289. for (i = 0; i < mca_cfg.banks; i++) {
  1290. struct mce_bank *b = &mce_banks[i];
  1291. if (!b->init)
  1292. continue;
  1293. wrmsrl(msr_ops.ctl(i), b->ctl);
  1294. wrmsrl(msr_ops.status(i), 0);
  1295. }
  1296. }
  1297. /*
  1298. * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
  1299. * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
  1300. * Vol 3B Table 15-20). But this confuses both the code that determines
  1301. * whether the machine check occurred in kernel or user mode, and also
  1302. * the severity assessment code. Pretend that EIPV was set, and take the
  1303. * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
  1304. */
  1305. static void quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs)
  1306. {
  1307. if (bank != 0)
  1308. return;
  1309. if ((m->mcgstatus & (MCG_STATUS_EIPV|MCG_STATUS_RIPV)) != 0)
  1310. return;
  1311. if ((m->status & (MCI_STATUS_OVER|MCI_STATUS_UC|
  1312. MCI_STATUS_EN|MCI_STATUS_MISCV|MCI_STATUS_ADDRV|
  1313. MCI_STATUS_PCC|MCI_STATUS_S|MCI_STATUS_AR|
  1314. MCACOD)) !=
  1315. (MCI_STATUS_UC|MCI_STATUS_EN|
  1316. MCI_STATUS_MISCV|MCI_STATUS_ADDRV|MCI_STATUS_S|
  1317. MCI_STATUS_AR|MCACOD_INSTR))
  1318. return;
  1319. m->mcgstatus |= MCG_STATUS_EIPV;
  1320. m->ip = regs->ip;
  1321. m->cs = regs->cs;
  1322. }
  1323. /* Add per CPU specific workarounds here */
  1324. static int __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
  1325. {
  1326. struct mca_config *cfg = &mca_cfg;
  1327. if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
  1328. pr_info("unknown CPU type - not enabling MCE support\n");
  1329. return -EOPNOTSUPP;
  1330. }
  1331. /* This should be disabled by the BIOS, but isn't always */
  1332. if (c->x86_vendor == X86_VENDOR_AMD) {
  1333. if (c->x86 == 15 && cfg->banks > 4) {
  1334. /*
  1335. * disable GART TBL walk error reporting, which
  1336. * trips off incorrectly with the IOMMU & 3ware
  1337. * & Cerberus:
  1338. */
  1339. clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
  1340. }
  1341. if (c->x86 < 0x11 && cfg->bootlog < 0) {
  1342. /*
  1343. * Lots of broken BIOS around that don't clear them
  1344. * by default and leave crap in there. Don't log:
  1345. */
  1346. cfg->bootlog = 0;
  1347. }
  1348. /*
  1349. * Various K7s with broken bank 0 around. Always disable
  1350. * by default.
  1351. */
  1352. if (c->x86 == 6 && cfg->banks > 0)
  1353. mce_banks[0].ctl = 0;
  1354. /*
  1355. * overflow_recov is supported for F15h Models 00h-0fh
  1356. * even though we don't have a CPUID bit for it.
  1357. */
  1358. if (c->x86 == 0x15 && c->x86_model <= 0xf)
  1359. mce_flags.overflow_recov = 1;
  1360. /*
  1361. * Turn off MC4_MISC thresholding banks on those models since
  1362. * they're not supported there.
  1363. */
  1364. if (c->x86 == 0x15 &&
  1365. (c->x86_model >= 0x10 && c->x86_model <= 0x1f)) {
  1366. int i;
  1367. u64 hwcr;
  1368. bool need_toggle;
  1369. u32 msrs[] = {
  1370. 0x00000413, /* MC4_MISC0 */
  1371. 0xc0000408, /* MC4_MISC1 */
  1372. };
  1373. rdmsrl(MSR_K7_HWCR, hwcr);
  1374. /* McStatusWrEn has to be set */
  1375. need_toggle = !(hwcr & BIT(18));
  1376. if (need_toggle)
  1377. wrmsrl(MSR_K7_HWCR, hwcr | BIT(18));
  1378. /* Clear CntP bit safely */
  1379. for (i = 0; i < ARRAY_SIZE(msrs); i++)
  1380. msr_clear_bit(msrs[i], 62);
  1381. /* restore old settings */
  1382. if (need_toggle)
  1383. wrmsrl(MSR_K7_HWCR, hwcr);
  1384. }
  1385. }
  1386. if (c->x86_vendor == X86_VENDOR_INTEL) {
  1387. /*
  1388. * SDM documents that on family 6 bank 0 should not be written
  1389. * because it aliases to another special BIOS controlled
  1390. * register.
  1391. * But it's not aliased anymore on model 0x1a+
  1392. * Don't ignore bank 0 completely because there could be a
  1393. * valid event later, merely don't write CTL0.
  1394. */
  1395. if (c->x86 == 6 && c->x86_model < 0x1A && cfg->banks > 0)
  1396. mce_banks[0].init = 0;
  1397. /*
  1398. * All newer Intel systems support MCE broadcasting. Enable
  1399. * synchronization with a one second timeout.
  1400. */
  1401. if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
  1402. cfg->monarch_timeout < 0)
  1403. cfg->monarch_timeout = USEC_PER_SEC;
  1404. /*
  1405. * There are also broken BIOSes on some Pentium M and
  1406. * earlier systems:
  1407. */
  1408. if (c->x86 == 6 && c->x86_model <= 13 && cfg->bootlog < 0)
  1409. cfg->bootlog = 0;
  1410. if (c->x86 == 6 && c->x86_model == 45)
  1411. quirk_no_way_out = quirk_sandybridge_ifu;
  1412. }
  1413. if (cfg->monarch_timeout < 0)
  1414. cfg->monarch_timeout = 0;
  1415. if (cfg->bootlog != 0)
  1416. cfg->panic_timeout = 30;
  1417. return 0;
  1418. }
  1419. static int __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
  1420. {
  1421. if (c->x86 != 5)
  1422. return 0;
  1423. switch (c->x86_vendor) {
  1424. case X86_VENDOR_INTEL:
  1425. intel_p5_mcheck_init(c);
  1426. return 1;
  1427. break;
  1428. case X86_VENDOR_CENTAUR:
  1429. winchip_mcheck_init(c);
  1430. return 1;
  1431. break;
  1432. default:
  1433. return 0;
  1434. }
  1435. return 0;
  1436. }
  1437. /*
  1438. * Init basic CPU features needed for early decoding of MCEs.
  1439. */
  1440. static void __mcheck_cpu_init_early(struct cpuinfo_x86 *c)
  1441. {
  1442. if (c->x86_vendor == X86_VENDOR_AMD) {
  1443. mce_flags.overflow_recov = !!cpu_has(c, X86_FEATURE_OVERFLOW_RECOV);
  1444. mce_flags.succor = !!cpu_has(c, X86_FEATURE_SUCCOR);
  1445. mce_flags.smca = !!cpu_has(c, X86_FEATURE_SMCA);
  1446. if (mce_flags.smca) {
  1447. msr_ops.ctl = smca_ctl_reg;
  1448. msr_ops.status = smca_status_reg;
  1449. msr_ops.addr = smca_addr_reg;
  1450. msr_ops.misc = smca_misc_reg;
  1451. }
  1452. }
  1453. }
  1454. static void mce_centaur_feature_init(struct cpuinfo_x86 *c)
  1455. {
  1456. struct mca_config *cfg = &mca_cfg;
  1457. /*
  1458. * All newer Centaur CPUs support MCE broadcasting. Enable
  1459. * synchronization with a one second timeout.
  1460. */
  1461. if ((c->x86 == 6 && c->x86_model == 0xf && c->x86_stepping >= 0xe) ||
  1462. c->x86 > 6) {
  1463. if (cfg->monarch_timeout < 0)
  1464. cfg->monarch_timeout = USEC_PER_SEC;
  1465. }
  1466. }
  1467. static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
  1468. {
  1469. switch (c->x86_vendor) {
  1470. case X86_VENDOR_INTEL:
  1471. mce_intel_feature_init(c);
  1472. mce_adjust_timer = cmci_intel_adjust_timer;
  1473. break;
  1474. case X86_VENDOR_AMD: {
  1475. mce_amd_feature_init(c);
  1476. break;
  1477. }
  1478. case X86_VENDOR_CENTAUR:
  1479. mce_centaur_feature_init(c);
  1480. break;
  1481. default:
  1482. break;
  1483. }
  1484. }
  1485. static void __mcheck_cpu_clear_vendor(struct cpuinfo_x86 *c)
  1486. {
  1487. switch (c->x86_vendor) {
  1488. case X86_VENDOR_INTEL:
  1489. mce_intel_feature_clear(c);
  1490. break;
  1491. default:
  1492. break;
  1493. }
  1494. }
  1495. static void mce_start_timer(struct timer_list *t)
  1496. {
  1497. unsigned long iv = check_interval * HZ;
  1498. if (mca_cfg.ignore_ce || !iv)
  1499. return;
  1500. this_cpu_write(mce_next_interval, iv);
  1501. __start_timer(t, iv);
  1502. }
  1503. static void __mcheck_cpu_setup_timer(void)
  1504. {
  1505. struct timer_list *t = this_cpu_ptr(&mce_timer);
  1506. timer_setup(t, mce_timer_fn, TIMER_PINNED);
  1507. }
  1508. static void __mcheck_cpu_init_timer(void)
  1509. {
  1510. struct timer_list *t = this_cpu_ptr(&mce_timer);
  1511. timer_setup(t, mce_timer_fn, TIMER_PINNED);
  1512. mce_start_timer(t);
  1513. }
  1514. /* Handle unconfigured int18 (should never happen) */
  1515. static void unexpected_machine_check(struct pt_regs *regs, long error_code)
  1516. {
  1517. pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
  1518. smp_processor_id());
  1519. }
  1520. /* Call the installed machine check handler for this CPU setup. */
  1521. void (*machine_check_vector)(struct pt_regs *, long error_code) =
  1522. unexpected_machine_check;
  1523. dotraplinkage void do_mce(struct pt_regs *regs, long error_code)
  1524. {
  1525. machine_check_vector(regs, error_code);
  1526. }
  1527. /*
  1528. * Called for each booted CPU to set up machine checks.
  1529. * Must be called with preempt off:
  1530. */
  1531. void mcheck_cpu_init(struct cpuinfo_x86 *c)
  1532. {
  1533. if (mca_cfg.disabled)
  1534. return;
  1535. if (__mcheck_cpu_ancient_init(c))
  1536. return;
  1537. if (!mce_available(c))
  1538. return;
  1539. if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
  1540. mca_cfg.disabled = 1;
  1541. return;
  1542. }
  1543. if (mce_gen_pool_init()) {
  1544. mca_cfg.disabled = 1;
  1545. pr_emerg("Couldn't allocate MCE records pool!\n");
  1546. return;
  1547. }
  1548. machine_check_vector = do_machine_check;
  1549. __mcheck_cpu_init_early(c);
  1550. __mcheck_cpu_init_generic();
  1551. __mcheck_cpu_init_vendor(c);
  1552. __mcheck_cpu_init_clear_banks();
  1553. __mcheck_cpu_setup_timer();
  1554. }
  1555. /*
  1556. * Called for each booted CPU to clear some machine checks opt-ins
  1557. */
  1558. void mcheck_cpu_clear(struct cpuinfo_x86 *c)
  1559. {
  1560. if (mca_cfg.disabled)
  1561. return;
  1562. if (!mce_available(c))
  1563. return;
  1564. /*
  1565. * Possibly to clear general settings generic to x86
  1566. * __mcheck_cpu_clear_generic(c);
  1567. */
  1568. __mcheck_cpu_clear_vendor(c);
  1569. }
  1570. static void __mce_disable_bank(void *arg)
  1571. {
  1572. int bank = *((int *)arg);
  1573. __clear_bit(bank, this_cpu_ptr(mce_poll_banks));
  1574. cmci_disable_bank(bank);
  1575. }
  1576. void mce_disable_bank(int bank)
  1577. {
  1578. if (bank >= mca_cfg.banks) {
  1579. pr_warn(FW_BUG
  1580. "Ignoring request to disable invalid MCA bank %d.\n",
  1581. bank);
  1582. return;
  1583. }
  1584. set_bit(bank, mce_banks_ce_disabled);
  1585. on_each_cpu(__mce_disable_bank, &bank, 1);
  1586. }
  1587. /*
  1588. * mce=off Disables machine check
  1589. * mce=no_cmci Disables CMCI
  1590. * mce=no_lmce Disables LMCE
  1591. * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
  1592. * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
  1593. * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
  1594. * monarchtimeout is how long to wait for other CPUs on machine
  1595. * check, or 0 to not wait
  1596. * mce=bootlog Log MCEs from before booting. Disabled by default on AMD Fam10h
  1597. and older.
  1598. * mce=nobootlog Don't log MCEs from before booting.
  1599. * mce=bios_cmci_threshold Don't program the CMCI threshold
  1600. * mce=recovery force enable memcpy_mcsafe()
  1601. */
  1602. static int __init mcheck_enable(char *str)
  1603. {
  1604. struct mca_config *cfg = &mca_cfg;
  1605. if (*str == 0) {
  1606. enable_p5_mce();
  1607. return 1;
  1608. }
  1609. if (*str == '=')
  1610. str++;
  1611. if (!strcmp(str, "off"))
  1612. cfg->disabled = 1;
  1613. else if (!strcmp(str, "no_cmci"))
  1614. cfg->cmci_disabled = true;
  1615. else if (!strcmp(str, "no_lmce"))
  1616. cfg->lmce_disabled = 1;
  1617. else if (!strcmp(str, "dont_log_ce"))
  1618. cfg->dont_log_ce = true;
  1619. else if (!strcmp(str, "ignore_ce"))
  1620. cfg->ignore_ce = true;
  1621. else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
  1622. cfg->bootlog = (str[0] == 'b');
  1623. else if (!strcmp(str, "bios_cmci_threshold"))
  1624. cfg->bios_cmci_threshold = 1;
  1625. else if (!strcmp(str, "recovery"))
  1626. cfg->recovery = 1;
  1627. else if (isdigit(str[0])) {
  1628. if (get_option(&str, &cfg->tolerant) == 2)
  1629. get_option(&str, &(cfg->monarch_timeout));
  1630. } else {
  1631. pr_info("mce argument %s ignored. Please use /sys\n", str);
  1632. return 0;
  1633. }
  1634. return 1;
  1635. }
  1636. __setup("mce", mcheck_enable);
  1637. int __init mcheck_init(void)
  1638. {
  1639. mcheck_intel_therm_init();
  1640. mce_register_decode_chain(&first_nb);
  1641. mce_register_decode_chain(&mce_srao_nb);
  1642. mce_register_decode_chain(&mce_default_nb);
  1643. mcheck_vendor_init_severity();
  1644. INIT_WORK(&mce_work, mce_gen_pool_process);
  1645. init_irq_work(&mce_irq_work, mce_irq_work_cb);
  1646. return 0;
  1647. }
  1648. /*
  1649. * mce_syscore: PM support
  1650. */
  1651. /*
  1652. * Disable machine checks on suspend and shutdown. We can't really handle
  1653. * them later.
  1654. */
  1655. static void mce_disable_error_reporting(void)
  1656. {
  1657. int i;
  1658. for (i = 0; i < mca_cfg.banks; i++) {
  1659. struct mce_bank *b = &mce_banks[i];
  1660. if (b->init)
  1661. wrmsrl(msr_ops.ctl(i), 0);
  1662. }
  1663. return;
  1664. }
  1665. static void vendor_disable_error_reporting(void)
  1666. {
  1667. /*
  1668. * Don't clear on Intel or AMD CPUs. Some of these MSRs are socket-wide.
  1669. * Disabling them for just a single offlined CPU is bad, since it will
  1670. * inhibit reporting for all shared resources on the socket like the
  1671. * last level cache (LLC), the integrated memory controller (iMC), etc.
  1672. */
  1673. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL ||
  1674. boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
  1675. return;
  1676. mce_disable_error_reporting();
  1677. }
  1678. static int mce_syscore_suspend(void)
  1679. {
  1680. vendor_disable_error_reporting();
  1681. return 0;
  1682. }
  1683. static void mce_syscore_shutdown(void)
  1684. {
  1685. vendor_disable_error_reporting();
  1686. }
  1687. /*
  1688. * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
  1689. * Only one CPU is active at this time, the others get re-added later using
  1690. * CPU hotplug:
  1691. */
  1692. static void mce_syscore_resume(void)
  1693. {
  1694. __mcheck_cpu_init_generic();
  1695. __mcheck_cpu_init_vendor(raw_cpu_ptr(&cpu_info));
  1696. __mcheck_cpu_init_clear_banks();
  1697. }
  1698. static struct syscore_ops mce_syscore_ops = {
  1699. .suspend = mce_syscore_suspend,
  1700. .shutdown = mce_syscore_shutdown,
  1701. .resume = mce_syscore_resume,
  1702. };
  1703. /*
  1704. * mce_device: Sysfs support
  1705. */
  1706. static void mce_cpu_restart(void *data)
  1707. {
  1708. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  1709. return;
  1710. __mcheck_cpu_init_generic();
  1711. __mcheck_cpu_init_clear_banks();
  1712. __mcheck_cpu_init_timer();
  1713. }
  1714. /* Reinit MCEs after user configuration changes */
  1715. static void mce_restart(void)
  1716. {
  1717. mce_timer_delete_all();
  1718. on_each_cpu(mce_cpu_restart, NULL, 1);
  1719. }
  1720. /* Toggle features for corrected errors */
  1721. static void mce_disable_cmci(void *data)
  1722. {
  1723. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  1724. return;
  1725. cmci_clear();
  1726. }
  1727. static void mce_enable_ce(void *all)
  1728. {
  1729. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  1730. return;
  1731. cmci_reenable();
  1732. cmci_recheck();
  1733. if (all)
  1734. __mcheck_cpu_init_timer();
  1735. }
  1736. static struct bus_type mce_subsys = {
  1737. .name = "machinecheck",
  1738. .dev_name = "machinecheck",
  1739. };
  1740. DEFINE_PER_CPU(struct device *, mce_device);
  1741. static inline struct mce_bank *attr_to_bank(struct device_attribute *attr)
  1742. {
  1743. return container_of(attr, struct mce_bank, attr);
  1744. }
  1745. static ssize_t show_bank(struct device *s, struct device_attribute *attr,
  1746. char *buf)
  1747. {
  1748. return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
  1749. }
  1750. static ssize_t set_bank(struct device *s, struct device_attribute *attr,
  1751. const char *buf, size_t size)
  1752. {
  1753. u64 new;
  1754. if (kstrtou64(buf, 0, &new) < 0)
  1755. return -EINVAL;
  1756. attr_to_bank(attr)->ctl = new;
  1757. mce_restart();
  1758. return size;
  1759. }
  1760. static ssize_t set_ignore_ce(struct device *s,
  1761. 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. mutex_lock(&mce_sysfs_mutex);
  1768. if (mca_cfg.ignore_ce ^ !!new) {
  1769. if (new) {
  1770. /* disable ce features */
  1771. mce_timer_delete_all();
  1772. on_each_cpu(mce_disable_cmci, NULL, 1);
  1773. mca_cfg.ignore_ce = true;
  1774. } else {
  1775. /* enable ce features */
  1776. mca_cfg.ignore_ce = false;
  1777. on_each_cpu(mce_enable_ce, (void *)1, 1);
  1778. }
  1779. }
  1780. mutex_unlock(&mce_sysfs_mutex);
  1781. return size;
  1782. }
  1783. static ssize_t set_cmci_disabled(struct device *s,
  1784. struct device_attribute *attr,
  1785. const char *buf, size_t size)
  1786. {
  1787. u64 new;
  1788. if (kstrtou64(buf, 0, &new) < 0)
  1789. return -EINVAL;
  1790. mutex_lock(&mce_sysfs_mutex);
  1791. if (mca_cfg.cmci_disabled ^ !!new) {
  1792. if (new) {
  1793. /* disable cmci */
  1794. on_each_cpu(mce_disable_cmci, NULL, 1);
  1795. mca_cfg.cmci_disabled = true;
  1796. } else {
  1797. /* enable cmci */
  1798. mca_cfg.cmci_disabled = false;
  1799. on_each_cpu(mce_enable_ce, NULL, 1);
  1800. }
  1801. }
  1802. mutex_unlock(&mce_sysfs_mutex);
  1803. return size;
  1804. }
  1805. static ssize_t store_int_with_restart(struct device *s,
  1806. struct device_attribute *attr,
  1807. const char *buf, size_t size)
  1808. {
  1809. unsigned long old_check_interval = check_interval;
  1810. ssize_t ret = device_store_ulong(s, attr, buf, size);
  1811. if (check_interval == old_check_interval)
  1812. return ret;
  1813. if (check_interval < 1)
  1814. check_interval = 1;
  1815. mutex_lock(&mce_sysfs_mutex);
  1816. mce_restart();
  1817. mutex_unlock(&mce_sysfs_mutex);
  1818. return ret;
  1819. }
  1820. static DEVICE_INT_ATTR(tolerant, 0644, mca_cfg.tolerant);
  1821. static DEVICE_INT_ATTR(monarch_timeout, 0644, mca_cfg.monarch_timeout);
  1822. static DEVICE_BOOL_ATTR(dont_log_ce, 0644, mca_cfg.dont_log_ce);
  1823. static struct dev_ext_attribute dev_attr_check_interval = {
  1824. __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
  1825. &check_interval
  1826. };
  1827. static struct dev_ext_attribute dev_attr_ignore_ce = {
  1828. __ATTR(ignore_ce, 0644, device_show_bool, set_ignore_ce),
  1829. &mca_cfg.ignore_ce
  1830. };
  1831. static struct dev_ext_attribute dev_attr_cmci_disabled = {
  1832. __ATTR(cmci_disabled, 0644, device_show_bool, set_cmci_disabled),
  1833. &mca_cfg.cmci_disabled
  1834. };
  1835. static struct device_attribute *mce_device_attrs[] = {
  1836. &dev_attr_tolerant.attr,
  1837. &dev_attr_check_interval.attr,
  1838. #ifdef CONFIG_X86_MCELOG_LEGACY
  1839. &dev_attr_trigger,
  1840. #endif
  1841. &dev_attr_monarch_timeout.attr,
  1842. &dev_attr_dont_log_ce.attr,
  1843. &dev_attr_ignore_ce.attr,
  1844. &dev_attr_cmci_disabled.attr,
  1845. NULL
  1846. };
  1847. static cpumask_var_t mce_device_initialized;
  1848. static void mce_device_release(struct device *dev)
  1849. {
  1850. kfree(dev);
  1851. }
  1852. /* Per cpu device init. All of the cpus still share the same ctrl bank: */
  1853. static int mce_device_create(unsigned int cpu)
  1854. {
  1855. struct device *dev;
  1856. int err;
  1857. int i, j;
  1858. if (!mce_available(&boot_cpu_data))
  1859. return -EIO;
  1860. dev = per_cpu(mce_device, cpu);
  1861. if (dev)
  1862. return 0;
  1863. dev = kzalloc(sizeof *dev, GFP_KERNEL);
  1864. if (!dev)
  1865. return -ENOMEM;
  1866. dev->id = cpu;
  1867. dev->bus = &mce_subsys;
  1868. dev->release = &mce_device_release;
  1869. err = device_register(dev);
  1870. if (err) {
  1871. put_device(dev);
  1872. return err;
  1873. }
  1874. for (i = 0; mce_device_attrs[i]; i++) {
  1875. err = device_create_file(dev, mce_device_attrs[i]);
  1876. if (err)
  1877. goto error;
  1878. }
  1879. for (j = 0; j < mca_cfg.banks; j++) {
  1880. err = device_create_file(dev, &mce_banks[j].attr);
  1881. if (err)
  1882. goto error2;
  1883. }
  1884. cpumask_set_cpu(cpu, mce_device_initialized);
  1885. per_cpu(mce_device, cpu) = dev;
  1886. return 0;
  1887. error2:
  1888. while (--j >= 0)
  1889. device_remove_file(dev, &mce_banks[j].attr);
  1890. error:
  1891. while (--i >= 0)
  1892. device_remove_file(dev, mce_device_attrs[i]);
  1893. device_unregister(dev);
  1894. return err;
  1895. }
  1896. static void mce_device_remove(unsigned int cpu)
  1897. {
  1898. struct device *dev = per_cpu(mce_device, cpu);
  1899. int i;
  1900. if (!cpumask_test_cpu(cpu, mce_device_initialized))
  1901. return;
  1902. for (i = 0; mce_device_attrs[i]; i++)
  1903. device_remove_file(dev, mce_device_attrs[i]);
  1904. for (i = 0; i < mca_cfg.banks; i++)
  1905. device_remove_file(dev, &mce_banks[i].attr);
  1906. device_unregister(dev);
  1907. cpumask_clear_cpu(cpu, mce_device_initialized);
  1908. per_cpu(mce_device, cpu) = NULL;
  1909. }
  1910. /* Make sure there are no machine checks on offlined CPUs. */
  1911. static void mce_disable_cpu(void)
  1912. {
  1913. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  1914. return;
  1915. if (!cpuhp_tasks_frozen)
  1916. cmci_clear();
  1917. vendor_disable_error_reporting();
  1918. }
  1919. static void mce_reenable_cpu(void)
  1920. {
  1921. int i;
  1922. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  1923. return;
  1924. if (!cpuhp_tasks_frozen)
  1925. cmci_reenable();
  1926. for (i = 0; i < mca_cfg.banks; i++) {
  1927. struct mce_bank *b = &mce_banks[i];
  1928. if (b->init)
  1929. wrmsrl(msr_ops.ctl(i), b->ctl);
  1930. }
  1931. }
  1932. static int mce_cpu_dead(unsigned int cpu)
  1933. {
  1934. mce_intel_hcpu_update(cpu);
  1935. /* intentionally ignoring frozen here */
  1936. if (!cpuhp_tasks_frozen)
  1937. cmci_rediscover();
  1938. return 0;
  1939. }
  1940. static int mce_cpu_online(unsigned int cpu)
  1941. {
  1942. struct timer_list *t = this_cpu_ptr(&mce_timer);
  1943. int ret;
  1944. mce_device_create(cpu);
  1945. ret = mce_threshold_create_device(cpu);
  1946. if (ret) {
  1947. mce_device_remove(cpu);
  1948. return ret;
  1949. }
  1950. mce_reenable_cpu();
  1951. mce_start_timer(t);
  1952. return 0;
  1953. }
  1954. static int mce_cpu_pre_down(unsigned int cpu)
  1955. {
  1956. struct timer_list *t = this_cpu_ptr(&mce_timer);
  1957. mce_disable_cpu();
  1958. del_timer_sync(t);
  1959. mce_threshold_remove_device(cpu);
  1960. mce_device_remove(cpu);
  1961. return 0;
  1962. }
  1963. static __init void mce_init_banks(void)
  1964. {
  1965. int i;
  1966. for (i = 0; i < mca_cfg.banks; i++) {
  1967. struct mce_bank *b = &mce_banks[i];
  1968. struct device_attribute *a = &b->attr;
  1969. sysfs_attr_init(&a->attr);
  1970. a->attr.name = b->attrname;
  1971. snprintf(b->attrname, ATTR_LEN, "bank%d", i);
  1972. a->attr.mode = 0644;
  1973. a->show = show_bank;
  1974. a->store = set_bank;
  1975. }
  1976. }
  1977. static __init int mcheck_init_device(void)
  1978. {
  1979. int err;
  1980. /*
  1981. * Check if we have a spare virtual bit. This will only become
  1982. * a problem if/when we move beyond 5-level page tables.
  1983. */
  1984. MAYBE_BUILD_BUG_ON(__VIRTUAL_MASK_SHIFT >= 63);
  1985. if (!mce_available(&boot_cpu_data)) {
  1986. err = -EIO;
  1987. goto err_out;
  1988. }
  1989. if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
  1990. err = -ENOMEM;
  1991. goto err_out;
  1992. }
  1993. mce_init_banks();
  1994. err = subsys_system_register(&mce_subsys, NULL);
  1995. if (err)
  1996. goto err_out_mem;
  1997. err = cpuhp_setup_state(CPUHP_X86_MCE_DEAD, "x86/mce:dead", NULL,
  1998. mce_cpu_dead);
  1999. if (err)
  2000. goto err_out_mem;
  2001. err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/mce:online",
  2002. mce_cpu_online, mce_cpu_pre_down);
  2003. if (err < 0)
  2004. goto err_out_online;
  2005. register_syscore_ops(&mce_syscore_ops);
  2006. return 0;
  2007. err_out_online:
  2008. cpuhp_remove_state(CPUHP_X86_MCE_DEAD);
  2009. err_out_mem:
  2010. free_cpumask_var(mce_device_initialized);
  2011. err_out:
  2012. pr_err("Unable to init MCE device (rc: %d)\n", err);
  2013. return err;
  2014. }
  2015. device_initcall_sync(mcheck_init_device);
  2016. /*
  2017. * Old style boot options parsing. Only for compatibility.
  2018. */
  2019. static int __init mcheck_disable(char *str)
  2020. {
  2021. mca_cfg.disabled = 1;
  2022. return 1;
  2023. }
  2024. __setup("nomce", mcheck_disable);
  2025. #ifdef CONFIG_DEBUG_FS
  2026. struct dentry *mce_get_debugfs_dir(void)
  2027. {
  2028. static struct dentry *dmce;
  2029. if (!dmce)
  2030. dmce = debugfs_create_dir("mce", NULL);
  2031. return dmce;
  2032. }
  2033. static void mce_reset(void)
  2034. {
  2035. cpu_missing = 0;
  2036. atomic_set(&mce_fake_panicked, 0);
  2037. atomic_set(&mce_executing, 0);
  2038. atomic_set(&mce_callin, 0);
  2039. atomic_set(&global_nwo, 0);
  2040. }
  2041. static int fake_panic_get(void *data, u64 *val)
  2042. {
  2043. *val = fake_panic;
  2044. return 0;
  2045. }
  2046. static int fake_panic_set(void *data, u64 val)
  2047. {
  2048. mce_reset();
  2049. fake_panic = val;
  2050. return 0;
  2051. }
  2052. DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
  2053. fake_panic_set, "%llu\n");
  2054. static int __init mcheck_debugfs_init(void)
  2055. {
  2056. struct dentry *dmce, *ffake_panic;
  2057. dmce = mce_get_debugfs_dir();
  2058. if (!dmce)
  2059. return -ENOMEM;
  2060. ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
  2061. &fake_panic_fops);
  2062. if (!ffake_panic)
  2063. return -ENOMEM;
  2064. return 0;
  2065. }
  2066. #else
  2067. static int __init mcheck_debugfs_init(void) { return -EINVAL; }
  2068. #endif
  2069. DEFINE_STATIC_KEY_FALSE(mcsafe_key);
  2070. EXPORT_SYMBOL_GPL(mcsafe_key);
  2071. static int __init mcheck_late_init(void)
  2072. {
  2073. if (mca_cfg.recovery)
  2074. static_branch_inc(&mcsafe_key);
  2075. mcheck_debugfs_init();
  2076. cec_init();
  2077. /*
  2078. * Flush out everything that has been logged during early boot, now that
  2079. * everything has been initialized (workqueues, decoders, ...).
  2080. */
  2081. mce_schedule_work();
  2082. return 0;
  2083. }
  2084. late_initcall(mcheck_late_init);