mce.c 63 KB

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