mce.c 62 KB

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