mce.c 57 KB

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