mce.c 54 KB

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