mce.c 57 KB

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