mce.c 62 KB

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