mce.c 59 KB

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