mce.c 57 KB

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