mirror of
https://github.com/raspberrypi/linux.git
synced 2025-12-06 01:49:46 +00:00
Using scripts/coccinelle/api/stream_open.cocci added in10dce8af34("fs: stream_open - opener for stream-like files so that read and write can run simultaneously without deadlock"), search and convert to stream_open all in-kernel nonseekable_open users for which read and write actually do not depend on ppos and where there is no other methods in file_operations which assume @offset access. I've verified each generated change manually - that it is correct to convert - and each other nonseekable_open instance left - that it is either not correct to convert there, or that it is not converted due to current stream_open.cocci limitations. The script also does not convert files that should be valid to convert, but that currently have .llseek = noop_llseek or generic_file_llseek for unknown reason despite file being opened with nonseekable_open (e.g. drivers/input/mousedev.c) Among cases converted 14 were potentially vulnerable to read vs write deadlock (see details in10dce8af34): drivers/char/pcmcia/cm4000_cs.c:1685:7-23: ERROR: cm4000_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. drivers/gnss/core.c:45:1-17: ERROR: gnss_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. drivers/hid/uhid.c:635:1-17: ERROR: uhid_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. drivers/infiniband/core/user_mad.c:988:1-17: ERROR: umad_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. drivers/input/evdev.c:527:1-17: ERROR: evdev_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. drivers/input/misc/uinput.c:401:1-17: ERROR: uinput_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. drivers/isdn/capi/capi.c:963:8-24: ERROR: capi_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. drivers/leds/uleds.c:77:1-17: ERROR: uleds_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. drivers/media/rc/lirc_dev.c:198:1-17: ERROR: lirc_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. drivers/s390/char/fs3270.c:488:1-17: ERROR: fs3270_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. drivers/usb/misc/ldusb.c:310:1-17: ERROR: ld_usb_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. drivers/xen/evtchn.c:667:8-24: ERROR: evtchn_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. net/batman-adv/icmp_socket.c:80:1-17: ERROR: batadv_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. net/rfkill/core.c:1146:8-24: ERROR: rfkill_fops: .read() can deadlock .write(); change nonseekable_open -> stream_open to fix. and the rest were just safe to convert to stream_open because their read and write do not use ppos at all and corresponding file_operations do not have methods that assume @offset file access(*): arch/powerpc/platforms/52xx/mpc52xx_gpt.c:631:8-24: WARNING: mpc52xx_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. arch/powerpc/platforms/cell/spufs/file.c:591:8-24: WARNING: spufs_ibox_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. arch/powerpc/platforms/cell/spufs/file.c:591:8-24: WARNING: spufs_ibox_stat_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. arch/powerpc/platforms/cell/spufs/file.c:591:8-24: WARNING: spufs_mbox_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. arch/powerpc/platforms/cell/spufs/file.c:591:8-24: WARNING: spufs_mbox_stat_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. arch/powerpc/platforms/cell/spufs/file.c:591:8-24: WARNING: spufs_wbox_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. arch/powerpc/platforms/cell/spufs/file.c:591:8-24: WARNING: spufs_wbox_stat_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. arch/um/drivers/harddog_kern.c:88:8-24: WARNING: harddog_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. arch/x86/kernel/cpu/microcode/core.c:430:33-49: WARNING: microcode_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/char/ds1620.c:215:8-24: WARNING: ds1620_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/char/dtlk.c:301:1-17: WARNING: dtlk_fops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. drivers/char/ipmi/ipmi_watchdog.c:840:9-25: WARNING: ipmi_wdog_fops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. drivers/char/pcmcia/scr24x_cs.c:95:8-24: WARNING: scr24x_fops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. drivers/char/tb0219.c:246:9-25: WARNING: tb0219_fops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. drivers/firewire/nosy.c:306:8-24: WARNING: nosy_ops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/hwmon/fschmd.c:840:8-24: WARNING: watchdog_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/hwmon/w83793.c:1344:8-24: WARNING: watchdog_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/infiniband/core/ucma.c:1747:8-24: WARNING: ucma_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/infiniband/core/ucm.c:1178:8-24: WARNING: ucm_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/infiniband/core/uverbs_main.c:1086:8-24: WARNING: uverbs_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/input/joydev.c:282:1-17: WARNING: joydev_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/pci/switch/switchtec.c:393:1-17: WARNING: switchtec_fops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. drivers/platform/chrome/cros_ec_debugfs.c:135:8-24: WARNING: cros_ec_console_log_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/rtc/rtc-ds1374.c:470:9-25: WARNING: ds1374_wdt_fops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. drivers/rtc/rtc-m41t80.c:805:9-25: WARNING: wdt_fops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. drivers/s390/char/tape_char.c:293:2-18: WARNING: tape_fops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. drivers/s390/char/zcore.c:194:8-24: WARNING: zcore_reipl_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/s390/crypto/zcrypt_api.c:528:8-24: WARNING: zcrypt_fops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. drivers/spi/spidev.c:594:1-17: WARNING: spidev_fops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. drivers/staging/pi433/pi433_if.c:974:1-17: WARNING: pi433_fops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/acquirewdt.c:203:8-24: WARNING: acq_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/advantechwdt.c:202:8-24: WARNING: advwdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/alim1535_wdt.c:252:8-24: WARNING: ali_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/alim7101_wdt.c:217:8-24: WARNING: wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/ar7_wdt.c:166:8-24: WARNING: ar7_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/at91rm9200_wdt.c:113:8-24: WARNING: at91wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/ath79_wdt.c:135:8-24: WARNING: ath79_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/bcm63xx_wdt.c:119:8-24: WARNING: bcm63xx_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/cpu5wdt.c:143:8-24: WARNING: cpu5wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/cpwd.c:397:8-24: WARNING: cpwd_fops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/eurotechwdt.c:319:8-24: WARNING: eurwdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/f71808e_wdt.c:528:8-24: WARNING: watchdog_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/gef_wdt.c:232:8-24: WARNING: gef_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/geodewdt.c:95:8-24: WARNING: geodewdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/ib700wdt.c:241:8-24: WARNING: ibwdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/ibmasr.c:326:8-24: WARNING: asr_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/indydog.c:80:8-24: WARNING: indydog_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/intel_scu_watchdog.c:307:8-24: WARNING: intel_scu_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/iop_wdt.c:104:8-24: WARNING: iop_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/it8712f_wdt.c:330:8-24: WARNING: it8712f_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/ixp4xx_wdt.c:68:8-24: WARNING: ixp4xx_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/ks8695_wdt.c:145:8-24: WARNING: ks8695wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/m54xx_wdt.c:88:8-24: WARNING: m54xx_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/machzwd.c:336:8-24: WARNING: zf_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/mixcomwd.c:153:8-24: WARNING: mixcomwd_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/mtx-1_wdt.c:121:8-24: WARNING: mtx1_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/mv64x60_wdt.c:136:8-24: WARNING: mv64x60_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/nuc900_wdt.c:134:8-24: WARNING: nuc900wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/nv_tco.c:164:8-24: WARNING: nv_tco_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/pc87413_wdt.c:289:8-24: WARNING: pc87413_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/pcwd.c:698:8-24: WARNING: pcwd_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/pcwd.c:737:8-24: WARNING: pcwd_temp_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/pcwd_pci.c:581:8-24: WARNING: pcipcwd_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/pcwd_pci.c:623:8-24: WARNING: pcipcwd_temp_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/pcwd_usb.c:488:8-24: WARNING: usb_pcwd_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/pcwd_usb.c:527:8-24: WARNING: usb_pcwd_temperature_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/pika_wdt.c:121:8-24: WARNING: pikawdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/pnx833x_wdt.c:119:8-24: WARNING: pnx833x_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/rc32434_wdt.c:153:8-24: WARNING: rc32434_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/rdc321x_wdt.c:145:8-24: WARNING: rdc321x_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/riowd.c:79:1-17: WARNING: riowd_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/sa1100_wdt.c:62:8-24: WARNING: sa1100dog_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/sbc60xxwdt.c:211:8-24: WARNING: wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/sbc7240_wdt.c:139:8-24: WARNING: wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/sbc8360.c:274:8-24: WARNING: sbc8360_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/sbc_epx_c3.c:81:8-24: WARNING: epx_c3_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/sbc_fitpc2_wdt.c:78:8-24: WARNING: fitpc2_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/sb_wdog.c:108:1-17: WARNING: sbwdog_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/sc1200wdt.c:181:8-24: WARNING: sc1200wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/sc520_wdt.c:261:8-24: WARNING: wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/sch311x_wdt.c:319:8-24: WARNING: sch311x_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/scx200_wdt.c:105:8-24: WARNING: scx200_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/smsc37b787_wdt.c:369:8-24: WARNING: wb_smsc_wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/w83877f_wdt.c:227:8-24: WARNING: wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/w83977f_wdt.c:301:8-24: WARNING: wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/wafer5823wdt.c:200:8-24: WARNING: wafwdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/watchdog_dev.c:828:8-24: WARNING: watchdog_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/wdrtas.c:379:8-24: WARNING: wdrtas_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/wdrtas.c:445:8-24: WARNING: wdrtas_temp_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/wdt285.c:104:1-17: WARNING: watchdog_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/wdt977.c:276:8-24: WARNING: wdt977_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/wdt.c:424:8-24: WARNING: wdt_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/wdt.c:484:8-24: WARNING: wdt_temp_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/wdt_pci.c:464:8-24: WARNING: wdtpci_fops: .write() has stream semantic; safe to change nonseekable_open -> stream_open. drivers/watchdog/wdt_pci.c:527:8-24: WARNING: wdtpci_temp_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. net/batman-adv/log.c:105:1-17: WARNING: batadv_log_fops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. sound/core/control.c:57:7-23: WARNING: snd_ctl_f_ops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. sound/core/rawmidi.c:385:7-23: WARNING: snd_rawmidi_f_ops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. sound/core/seq/seq_clientmgr.c:310:7-23: WARNING: snd_seq_f_ops: .read() and .write() have stream semantic; safe to change nonseekable_open -> stream_open. sound/core/timer.c:1428:7-23: WARNING: snd_timer_f_ops: .read() has stream semantic; safe to change nonseekable_open -> stream_open. One can also recheck/review the patch via generating it with explanation comments included via $ make coccicheck MODE=patch COCCI=scripts/coccinelle/api/stream_open.cocci SPFLAGS="-D explain" (*) This second group also contains cases with read/write deadlocks that stream_open.cocci don't yet detect, but which are still valid to convert to stream_open since ppos is not used. For example drivers/pci/switch/switchtec.c calls wait_for_completion_interruptible() in its .read, but stream_open.cocci currently detects only "wait_event*" as blocking. Cc: Michael Kerrisk <mtk.manpages@gmail.com> Cc: Yongzhi Pan <panyongzhi@gmail.com> Cc: Jonathan Corbet <corbet@lwn.net> Cc: David Vrabel <david.vrabel@citrix.com> Cc: Juergen Gross <jgross@suse.com> Cc: Miklos Szeredi <miklos@szeredi.hu> Cc: Tejun Heo <tj@kernel.org> Cc: Kirill Tkhai <ktkhai@virtuozzo.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Christoph Hellwig <hch@lst.de> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Julia Lawall <Julia.Lawall@lip6.fr> Cc: Nikolaus Rath <Nikolaus@rath.org> Cc: Han-Wen Nienhuys <hanwen@google.com> Cc: Anatolij Gustschin <agust@denx.de> Cc: Jeff Dike <jdike@addtoit.com> Cc: Richard Weinberger <richard@nod.at> Cc: Anton Ivanov <anton.ivanov@cambridgegreys.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Ingo Molnar <mingo@redhat.com> Cc: "James R. Van Zandt" <jrv@vanzandt.mv.com> Cc: Corey Minyard <minyard@acm.org> Cc: Harald Welte <laforge@gnumonks.org> Acked-by: Lubomir Rintel <lkundrak@v3.sk> [scr24x_cs] Cc: Stefan Richter <stefanr@s5r6.in-berlin.de> Cc: Johan Hovold <johan@kernel.org> Cc: David Herrmann <dh.herrmann@googlemail.com> Cc: Jiri Kosina <jikos@kernel.org> Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com> Cc: Jean Delvare <jdelvare@suse.com> Acked-by: Guenter Roeck <linux@roeck-us.net> [watchdog/* hwmon/*] Cc: Rudolf Marek <r.marek@assembler.cz> Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com> Cc: Karsten Keil <isdn@linux-pingi.de> Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com> Cc: Pavel Machek <pavel@ucw.cz> Cc: Mauro Carvalho Chehab <mchehab@kernel.org> Cc: Kurt Schwemmer <kurt.schwemmer@microsemi.com> Acked-by: Logan Gunthorpe <logang@deltatee.com> [drivers/pci/switch/switchtec] Acked-by: Bjorn Helgaas <bhelgaas@google.com> [drivers/pci/switch/switchtec] Cc: Benson Leung <bleung@chromium.org> Acked-by: Enric Balletbo i Serra <enric.balletbo@collabora.com> [platform/chrome] Cc: Alessandro Zummo <a.zummo@towertech.it> Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com> [rtc/*] Cc: Mark Brown <broonie@kernel.org> Cc: Wim Van Sebroeck <wim@linux-watchdog.org> Cc: Florian Fainelli <f.fainelli@gmail.com> Cc: bcm-kernel-feedback-list@broadcom.com Cc: Wan ZongShun <mcuos.com@gmail.com> Cc: Zwane Mwaikambo <zwanem@gmail.com> Cc: Marek Lindner <mareklindner@neomailbox.ch> Cc: Simon Wunderlich <sw@simonwunderlich.de> Cc: Antonio Quartulli <a@unstable.cc> Cc: "David S. Miller" <davem@davemloft.net> Cc: Johannes Berg <johannes@sipsolutions.net> Cc: Jaroslav Kysela <perex@perex.cz> Cc: Takashi Iwai <tiwai@suse.com> Signed-off-by: Kirill Smelkov <kirr@nexedi.com>
905 lines
19 KiB
C
905 lines
19 KiB
C
/*
|
|
* CPU Microcode Update Driver for Linux
|
|
*
|
|
* Copyright (C) 2000-2006 Tigran Aivazian <aivazian.tigran@gmail.com>
|
|
* 2006 Shaohua Li <shaohua.li@intel.com>
|
|
* 2013-2016 Borislav Petkov <bp@alien8.de>
|
|
*
|
|
* X86 CPU microcode early update for Linux:
|
|
*
|
|
* Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
|
|
* H Peter Anvin" <hpa@zytor.com>
|
|
* (C) 2015 Borislav Petkov <bp@alien8.de>
|
|
*
|
|
* This driver allows to upgrade microcode on x86 processors.
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version
|
|
* 2 of the License, or (at your option) any later version.
|
|
*/
|
|
|
|
#define pr_fmt(fmt) "microcode: " fmt
|
|
|
|
#include <linux/platform_device.h>
|
|
#include <linux/stop_machine.h>
|
|
#include <linux/syscore_ops.h>
|
|
#include <linux/miscdevice.h>
|
|
#include <linux/capability.h>
|
|
#include <linux/firmware.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/delay.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/cpu.h>
|
|
#include <linux/nmi.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/mm.h>
|
|
|
|
#include <asm/microcode_intel.h>
|
|
#include <asm/cpu_device_id.h>
|
|
#include <asm/microcode_amd.h>
|
|
#include <asm/perf_event.h>
|
|
#include <asm/microcode.h>
|
|
#include <asm/processor.h>
|
|
#include <asm/cmdline.h>
|
|
#include <asm/setup.h>
|
|
|
|
#define DRIVER_VERSION "2.2"
|
|
|
|
static struct microcode_ops *microcode_ops;
|
|
static bool dis_ucode_ldr = true;
|
|
|
|
bool initrd_gone;
|
|
|
|
LIST_HEAD(microcode_cache);
|
|
|
|
/*
|
|
* Synchronization.
|
|
*
|
|
* All non cpu-hotplug-callback call sites use:
|
|
*
|
|
* - microcode_mutex to synchronize with each other;
|
|
* - get/put_online_cpus() to synchronize with
|
|
* the cpu-hotplug-callback call sites.
|
|
*
|
|
* We guarantee that only a single cpu is being
|
|
* updated at any particular moment of time.
|
|
*/
|
|
static DEFINE_MUTEX(microcode_mutex);
|
|
|
|
/*
|
|
* Serialize late loading so that CPUs get updated one-by-one.
|
|
*/
|
|
static DEFINE_RAW_SPINLOCK(update_lock);
|
|
|
|
struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
|
|
|
|
struct cpu_info_ctx {
|
|
struct cpu_signature *cpu_sig;
|
|
int err;
|
|
};
|
|
|
|
/*
|
|
* Those patch levels cannot be updated to newer ones and thus should be final.
|
|
*/
|
|
static u32 final_levels[] = {
|
|
0x01000098,
|
|
0x0100009f,
|
|
0x010000af,
|
|
0, /* T-101 terminator */
|
|
};
|
|
|
|
/*
|
|
* Check the current patch level on this CPU.
|
|
*
|
|
* Returns:
|
|
* - true: if update should stop
|
|
* - false: otherwise
|
|
*/
|
|
static bool amd_check_current_patch_level(void)
|
|
{
|
|
u32 lvl, dummy, i;
|
|
u32 *levels;
|
|
|
|
native_rdmsr(MSR_AMD64_PATCH_LEVEL, lvl, dummy);
|
|
|
|
if (IS_ENABLED(CONFIG_X86_32))
|
|
levels = (u32 *)__pa_nodebug(&final_levels);
|
|
else
|
|
levels = final_levels;
|
|
|
|
for (i = 0; levels[i]; i++) {
|
|
if (lvl == levels[i])
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static bool __init check_loader_disabled_bsp(void)
|
|
{
|
|
static const char *__dis_opt_str = "dis_ucode_ldr";
|
|
|
|
#ifdef CONFIG_X86_32
|
|
const char *cmdline = (const char *)__pa_nodebug(boot_command_line);
|
|
const char *option = (const char *)__pa_nodebug(__dis_opt_str);
|
|
bool *res = (bool *)__pa_nodebug(&dis_ucode_ldr);
|
|
|
|
#else /* CONFIG_X86_64 */
|
|
const char *cmdline = boot_command_line;
|
|
const char *option = __dis_opt_str;
|
|
bool *res = &dis_ucode_ldr;
|
|
#endif
|
|
|
|
/*
|
|
* CPUID(1).ECX[31]: reserved for hypervisor use. This is still not
|
|
* completely accurate as xen pv guests don't see that CPUID bit set but
|
|
* that's good enough as they don't land on the BSP path anyway.
|
|
*/
|
|
if (native_cpuid_ecx(1) & BIT(31))
|
|
return *res;
|
|
|
|
if (x86_cpuid_vendor() == X86_VENDOR_AMD) {
|
|
if (amd_check_current_patch_level())
|
|
return *res;
|
|
}
|
|
|
|
if (cmdline_find_option_bool(cmdline, option) <= 0)
|
|
*res = false;
|
|
|
|
return *res;
|
|
}
|
|
|
|
extern struct builtin_fw __start_builtin_fw[];
|
|
extern struct builtin_fw __end_builtin_fw[];
|
|
|
|
bool get_builtin_firmware(struct cpio_data *cd, const char *name)
|
|
{
|
|
#ifdef CONFIG_FW_LOADER
|
|
struct builtin_fw *b_fw;
|
|
|
|
for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
|
|
if (!strcmp(name, b_fw->name)) {
|
|
cd->size = b_fw->size;
|
|
cd->data = b_fw->data;
|
|
return true;
|
|
}
|
|
}
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
void __init load_ucode_bsp(void)
|
|
{
|
|
unsigned int cpuid_1_eax;
|
|
bool intel = true;
|
|
|
|
if (!have_cpuid_p())
|
|
return;
|
|
|
|
cpuid_1_eax = native_cpuid_eax(1);
|
|
|
|
switch (x86_cpuid_vendor()) {
|
|
case X86_VENDOR_INTEL:
|
|
if (x86_family(cpuid_1_eax) < 6)
|
|
return;
|
|
break;
|
|
|
|
case X86_VENDOR_AMD:
|
|
if (x86_family(cpuid_1_eax) < 0x10)
|
|
return;
|
|
intel = false;
|
|
break;
|
|
|
|
default:
|
|
return;
|
|
}
|
|
|
|
if (check_loader_disabled_bsp())
|
|
return;
|
|
|
|
if (intel)
|
|
load_ucode_intel_bsp();
|
|
else
|
|
load_ucode_amd_bsp(cpuid_1_eax);
|
|
}
|
|
|
|
static bool check_loader_disabled_ap(void)
|
|
{
|
|
#ifdef CONFIG_X86_32
|
|
return *((bool *)__pa_nodebug(&dis_ucode_ldr));
|
|
#else
|
|
return dis_ucode_ldr;
|
|
#endif
|
|
}
|
|
|
|
void load_ucode_ap(void)
|
|
{
|
|
unsigned int cpuid_1_eax;
|
|
|
|
if (check_loader_disabled_ap())
|
|
return;
|
|
|
|
cpuid_1_eax = native_cpuid_eax(1);
|
|
|
|
switch (x86_cpuid_vendor()) {
|
|
case X86_VENDOR_INTEL:
|
|
if (x86_family(cpuid_1_eax) >= 6)
|
|
load_ucode_intel_ap();
|
|
break;
|
|
case X86_VENDOR_AMD:
|
|
if (x86_family(cpuid_1_eax) >= 0x10)
|
|
load_ucode_amd_ap(cpuid_1_eax);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static int __init save_microcode_in_initrd(void)
|
|
{
|
|
struct cpuinfo_x86 *c = &boot_cpu_data;
|
|
int ret = -EINVAL;
|
|
|
|
switch (c->x86_vendor) {
|
|
case X86_VENDOR_INTEL:
|
|
if (c->x86 >= 6)
|
|
ret = save_microcode_in_initrd_intel();
|
|
break;
|
|
case X86_VENDOR_AMD:
|
|
if (c->x86 >= 0x10)
|
|
ret = save_microcode_in_initrd_amd(cpuid_eax(1));
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
initrd_gone = true;
|
|
|
|
return ret;
|
|
}
|
|
|
|
struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa)
|
|
{
|
|
#ifdef CONFIG_BLK_DEV_INITRD
|
|
unsigned long start = 0;
|
|
size_t size;
|
|
|
|
#ifdef CONFIG_X86_32
|
|
struct boot_params *params;
|
|
|
|
if (use_pa)
|
|
params = (struct boot_params *)__pa_nodebug(&boot_params);
|
|
else
|
|
params = &boot_params;
|
|
|
|
size = params->hdr.ramdisk_size;
|
|
|
|
/*
|
|
* Set start only if we have an initrd image. We cannot use initrd_start
|
|
* because it is not set that early yet.
|
|
*/
|
|
if (size)
|
|
start = params->hdr.ramdisk_image;
|
|
|
|
# else /* CONFIG_X86_64 */
|
|
size = (unsigned long)boot_params.ext_ramdisk_size << 32;
|
|
size |= boot_params.hdr.ramdisk_size;
|
|
|
|
if (size) {
|
|
start = (unsigned long)boot_params.ext_ramdisk_image << 32;
|
|
start |= boot_params.hdr.ramdisk_image;
|
|
|
|
start += PAGE_OFFSET;
|
|
}
|
|
# endif
|
|
|
|
/*
|
|
* Fixup the start address: after reserve_initrd() runs, initrd_start
|
|
* has the virtual address of the beginning of the initrd. It also
|
|
* possibly relocates the ramdisk. In either case, initrd_start contains
|
|
* the updated address so use that instead.
|
|
*
|
|
* initrd_gone is for the hotplug case where we've thrown out initrd
|
|
* already.
|
|
*/
|
|
if (!use_pa) {
|
|
if (initrd_gone)
|
|
return (struct cpio_data){ NULL, 0, "" };
|
|
if (initrd_start)
|
|
start = initrd_start;
|
|
} else {
|
|
/*
|
|
* The picture with physical addresses is a bit different: we
|
|
* need to get the *physical* address to which the ramdisk was
|
|
* relocated, i.e., relocated_ramdisk (not initrd_start) and
|
|
* since we're running from physical addresses, we need to access
|
|
* relocated_ramdisk through its *physical* address too.
|
|
*/
|
|
u64 *rr = (u64 *)__pa_nodebug(&relocated_ramdisk);
|
|
if (*rr)
|
|
start = *rr;
|
|
}
|
|
|
|
return find_cpio_data(path, (void *)start, size, NULL);
|
|
#else /* !CONFIG_BLK_DEV_INITRD */
|
|
return (struct cpio_data){ NULL, 0, "" };
|
|
#endif
|
|
}
|
|
|
|
void reload_early_microcode(void)
|
|
{
|
|
int vendor, family;
|
|
|
|
vendor = x86_cpuid_vendor();
|
|
family = x86_cpuid_family();
|
|
|
|
switch (vendor) {
|
|
case X86_VENDOR_INTEL:
|
|
if (family >= 6)
|
|
reload_ucode_intel();
|
|
break;
|
|
case X86_VENDOR_AMD:
|
|
if (family >= 0x10)
|
|
reload_ucode_amd();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void collect_cpu_info_local(void *arg)
|
|
{
|
|
struct cpu_info_ctx *ctx = arg;
|
|
|
|
ctx->err = microcode_ops->collect_cpu_info(smp_processor_id(),
|
|
ctx->cpu_sig);
|
|
}
|
|
|
|
static int collect_cpu_info_on_target(int cpu, struct cpu_signature *cpu_sig)
|
|
{
|
|
struct cpu_info_ctx ctx = { .cpu_sig = cpu_sig, .err = 0 };
|
|
int ret;
|
|
|
|
ret = smp_call_function_single(cpu, collect_cpu_info_local, &ctx, 1);
|
|
if (!ret)
|
|
ret = ctx.err;
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int collect_cpu_info(int cpu)
|
|
{
|
|
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
|
|
int ret;
|
|
|
|
memset(uci, 0, sizeof(*uci));
|
|
|
|
ret = collect_cpu_info_on_target(cpu, &uci->cpu_sig);
|
|
if (!ret)
|
|
uci->valid = 1;
|
|
|
|
return ret;
|
|
}
|
|
|
|
static void apply_microcode_local(void *arg)
|
|
{
|
|
enum ucode_state *err = arg;
|
|
|
|
*err = microcode_ops->apply_microcode(smp_processor_id());
|
|
}
|
|
|
|
static int apply_microcode_on_target(int cpu)
|
|
{
|
|
enum ucode_state err;
|
|
int ret;
|
|
|
|
ret = smp_call_function_single(cpu, apply_microcode_local, &err, 1);
|
|
if (!ret) {
|
|
if (err == UCODE_ERROR)
|
|
ret = 1;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
#ifdef CONFIG_MICROCODE_OLD_INTERFACE
|
|
static int do_microcode_update(const void __user *buf, size_t size)
|
|
{
|
|
int error = 0;
|
|
int cpu;
|
|
|
|
for_each_online_cpu(cpu) {
|
|
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
|
|
enum ucode_state ustate;
|
|
|
|
if (!uci->valid)
|
|
continue;
|
|
|
|
ustate = microcode_ops->request_microcode_user(cpu, buf, size);
|
|
if (ustate == UCODE_ERROR) {
|
|
error = -1;
|
|
break;
|
|
} else if (ustate == UCODE_OK)
|
|
apply_microcode_on_target(cpu);
|
|
}
|
|
|
|
return error;
|
|
}
|
|
|
|
static int microcode_open(struct inode *inode, struct file *file)
|
|
{
|
|
return capable(CAP_SYS_RAWIO) ? stream_open(inode, file) : -EPERM;
|
|
}
|
|
|
|
static ssize_t microcode_write(struct file *file, const char __user *buf,
|
|
size_t len, loff_t *ppos)
|
|
{
|
|
ssize_t ret = -EINVAL;
|
|
unsigned long nr_pages = totalram_pages();
|
|
|
|
if ((len >> PAGE_SHIFT) > nr_pages) {
|
|
pr_err("too much data (max %ld pages)\n", nr_pages);
|
|
return ret;
|
|
}
|
|
|
|
get_online_cpus();
|
|
mutex_lock(µcode_mutex);
|
|
|
|
if (do_microcode_update(buf, len) == 0)
|
|
ret = (ssize_t)len;
|
|
|
|
if (ret > 0)
|
|
perf_check_microcode();
|
|
|
|
mutex_unlock(µcode_mutex);
|
|
put_online_cpus();
|
|
|
|
return ret;
|
|
}
|
|
|
|
static const struct file_operations microcode_fops = {
|
|
.owner = THIS_MODULE,
|
|
.write = microcode_write,
|
|
.open = microcode_open,
|
|
.llseek = no_llseek,
|
|
};
|
|
|
|
static struct miscdevice microcode_dev = {
|
|
.minor = MICROCODE_MINOR,
|
|
.name = "microcode",
|
|
.nodename = "cpu/microcode",
|
|
.fops = µcode_fops,
|
|
};
|
|
|
|
static int __init microcode_dev_init(void)
|
|
{
|
|
int error;
|
|
|
|
error = misc_register(µcode_dev);
|
|
if (error) {
|
|
pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR);
|
|
return error;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void __exit microcode_dev_exit(void)
|
|
{
|
|
misc_deregister(µcode_dev);
|
|
}
|
|
#else
|
|
#define microcode_dev_init() 0
|
|
#define microcode_dev_exit() do { } while (0)
|
|
#endif
|
|
|
|
/* fake device for request_firmware */
|
|
static struct platform_device *microcode_pdev;
|
|
|
|
/*
|
|
* Late loading dance. Why the heavy-handed stomp_machine effort?
|
|
*
|
|
* - HT siblings must be idle and not execute other code while the other sibling
|
|
* is loading microcode in order to avoid any negative interactions caused by
|
|
* the loading.
|
|
*
|
|
* - In addition, microcode update on the cores must be serialized until this
|
|
* requirement can be relaxed in the future. Right now, this is conservative
|
|
* and good.
|
|
*/
|
|
#define SPINUNIT 100 /* 100 nsec */
|
|
|
|
static int check_online_cpus(void)
|
|
{
|
|
unsigned int cpu;
|
|
|
|
/*
|
|
* Make sure all CPUs are online. It's fine for SMT to be disabled if
|
|
* all the primary threads are still online.
|
|
*/
|
|
for_each_present_cpu(cpu) {
|
|
if (topology_is_primary_thread(cpu) && !cpu_online(cpu)) {
|
|
pr_err("Not all CPUs online, aborting microcode update.\n");
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static atomic_t late_cpus_in;
|
|
static atomic_t late_cpus_out;
|
|
|
|
static int __wait_for_cpus(atomic_t *t, long long timeout)
|
|
{
|
|
int all_cpus = num_online_cpus();
|
|
|
|
atomic_inc(t);
|
|
|
|
while (atomic_read(t) < all_cpus) {
|
|
if (timeout < SPINUNIT) {
|
|
pr_err("Timeout while waiting for CPUs rendezvous, remaining: %d\n",
|
|
all_cpus - atomic_read(t));
|
|
return 1;
|
|
}
|
|
|
|
ndelay(SPINUNIT);
|
|
timeout -= SPINUNIT;
|
|
|
|
touch_nmi_watchdog();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Returns:
|
|
* < 0 - on error
|
|
* 0 - no update done
|
|
* 1 - microcode was updated
|
|
*/
|
|
static int __reload_late(void *info)
|
|
{
|
|
int cpu = smp_processor_id();
|
|
enum ucode_state err;
|
|
int ret = 0;
|
|
|
|
/*
|
|
* Wait for all CPUs to arrive. A load will not be attempted unless all
|
|
* CPUs show up.
|
|
* */
|
|
if (__wait_for_cpus(&late_cpus_in, NSEC_PER_SEC))
|
|
return -1;
|
|
|
|
raw_spin_lock(&update_lock);
|
|
apply_microcode_local(&err);
|
|
raw_spin_unlock(&update_lock);
|
|
|
|
/* siblings return UCODE_OK because their engine got updated already */
|
|
if (err > UCODE_NFOUND) {
|
|
pr_warn("Error reloading microcode on CPU %d\n", cpu);
|
|
ret = -1;
|
|
} else if (err == UCODE_UPDATED || err == UCODE_OK) {
|
|
ret = 1;
|
|
}
|
|
|
|
/*
|
|
* Increase the wait timeout to a safe value here since we're
|
|
* serializing the microcode update and that could take a while on a
|
|
* large number of CPUs. And that is fine as the *actual* timeout will
|
|
* be determined by the last CPU finished updating and thus cut short.
|
|
*/
|
|
if (__wait_for_cpus(&late_cpus_out, NSEC_PER_SEC * num_online_cpus()))
|
|
panic("Timeout during microcode update!\n");
|
|
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Reload microcode late on all CPUs. Wait for a sec until they
|
|
* all gather together.
|
|
*/
|
|
static int microcode_reload_late(void)
|
|
{
|
|
int ret;
|
|
|
|
atomic_set(&late_cpus_in, 0);
|
|
atomic_set(&late_cpus_out, 0);
|
|
|
|
ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask);
|
|
if (ret > 0)
|
|
microcode_check();
|
|
|
|
pr_info("Reload completed, microcode revision: 0x%x\n", boot_cpu_data.microcode);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static ssize_t reload_store(struct device *dev,
|
|
struct device_attribute *attr,
|
|
const char *buf, size_t size)
|
|
{
|
|
enum ucode_state tmp_ret = UCODE_OK;
|
|
int bsp = boot_cpu_data.cpu_index;
|
|
unsigned long val;
|
|
ssize_t ret = 0;
|
|
|
|
ret = kstrtoul(buf, 0, &val);
|
|
if (ret)
|
|
return ret;
|
|
|
|
if (val != 1)
|
|
return size;
|
|
|
|
tmp_ret = microcode_ops->request_microcode_fw(bsp, µcode_pdev->dev, true);
|
|
if (tmp_ret != UCODE_NEW)
|
|
return size;
|
|
|
|
get_online_cpus();
|
|
|
|
ret = check_online_cpus();
|
|
if (ret)
|
|
goto put;
|
|
|
|
mutex_lock(µcode_mutex);
|
|
ret = microcode_reload_late();
|
|
mutex_unlock(µcode_mutex);
|
|
|
|
put:
|
|
put_online_cpus();
|
|
|
|
if (ret >= 0)
|
|
ret = size;
|
|
|
|
return ret;
|
|
}
|
|
|
|
static ssize_t version_show(struct device *dev,
|
|
struct device_attribute *attr, char *buf)
|
|
{
|
|
struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
|
|
|
|
return sprintf(buf, "0x%x\n", uci->cpu_sig.rev);
|
|
}
|
|
|
|
static ssize_t pf_show(struct device *dev,
|
|
struct device_attribute *attr, char *buf)
|
|
{
|
|
struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
|
|
|
|
return sprintf(buf, "0x%x\n", uci->cpu_sig.pf);
|
|
}
|
|
|
|
static DEVICE_ATTR_WO(reload);
|
|
static DEVICE_ATTR(version, 0444, version_show, NULL);
|
|
static DEVICE_ATTR(processor_flags, 0444, pf_show, NULL);
|
|
|
|
static struct attribute *mc_default_attrs[] = {
|
|
&dev_attr_version.attr,
|
|
&dev_attr_processor_flags.attr,
|
|
NULL
|
|
};
|
|
|
|
static const struct attribute_group mc_attr_group = {
|
|
.attrs = mc_default_attrs,
|
|
.name = "microcode",
|
|
};
|
|
|
|
static void microcode_fini_cpu(int cpu)
|
|
{
|
|
if (microcode_ops->microcode_fini_cpu)
|
|
microcode_ops->microcode_fini_cpu(cpu);
|
|
}
|
|
|
|
static enum ucode_state microcode_resume_cpu(int cpu)
|
|
{
|
|
if (apply_microcode_on_target(cpu))
|
|
return UCODE_ERROR;
|
|
|
|
pr_debug("CPU%d updated upon resume\n", cpu);
|
|
|
|
return UCODE_OK;
|
|
}
|
|
|
|
static enum ucode_state microcode_init_cpu(int cpu, bool refresh_fw)
|
|
{
|
|
enum ucode_state ustate;
|
|
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
|
|
|
|
if (uci->valid)
|
|
return UCODE_OK;
|
|
|
|
if (collect_cpu_info(cpu))
|
|
return UCODE_ERROR;
|
|
|
|
/* --dimm. Trigger a delayed update? */
|
|
if (system_state != SYSTEM_RUNNING)
|
|
return UCODE_NFOUND;
|
|
|
|
ustate = microcode_ops->request_microcode_fw(cpu, µcode_pdev->dev, refresh_fw);
|
|
if (ustate == UCODE_NEW) {
|
|
pr_debug("CPU%d updated upon init\n", cpu);
|
|
apply_microcode_on_target(cpu);
|
|
}
|
|
|
|
return ustate;
|
|
}
|
|
|
|
static enum ucode_state microcode_update_cpu(int cpu)
|
|
{
|
|
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
|
|
|
|
/* Refresh CPU microcode revision after resume. */
|
|
collect_cpu_info(cpu);
|
|
|
|
if (uci->valid)
|
|
return microcode_resume_cpu(cpu);
|
|
|
|
return microcode_init_cpu(cpu, false);
|
|
}
|
|
|
|
static int mc_device_add(struct device *dev, struct subsys_interface *sif)
|
|
{
|
|
int err, cpu = dev->id;
|
|
|
|
if (!cpu_online(cpu))
|
|
return 0;
|
|
|
|
pr_debug("CPU%d added\n", cpu);
|
|
|
|
err = sysfs_create_group(&dev->kobj, &mc_attr_group);
|
|
if (err)
|
|
return err;
|
|
|
|
if (microcode_init_cpu(cpu, true) == UCODE_ERROR)
|
|
return -EINVAL;
|
|
|
|
return err;
|
|
}
|
|
|
|
static void mc_device_remove(struct device *dev, struct subsys_interface *sif)
|
|
{
|
|
int cpu = dev->id;
|
|
|
|
if (!cpu_online(cpu))
|
|
return;
|
|
|
|
pr_debug("CPU%d removed\n", cpu);
|
|
microcode_fini_cpu(cpu);
|
|
sysfs_remove_group(&dev->kobj, &mc_attr_group);
|
|
}
|
|
|
|
static struct subsys_interface mc_cpu_interface = {
|
|
.name = "microcode",
|
|
.subsys = &cpu_subsys,
|
|
.add_dev = mc_device_add,
|
|
.remove_dev = mc_device_remove,
|
|
};
|
|
|
|
/**
|
|
* mc_bp_resume - Update boot CPU microcode during resume.
|
|
*/
|
|
static void mc_bp_resume(void)
|
|
{
|
|
int cpu = smp_processor_id();
|
|
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
|
|
|
|
if (uci->valid && uci->mc)
|
|
microcode_ops->apply_microcode(cpu);
|
|
else if (!uci->mc)
|
|
reload_early_microcode();
|
|
}
|
|
|
|
static struct syscore_ops mc_syscore_ops = {
|
|
.resume = mc_bp_resume,
|
|
};
|
|
|
|
static int mc_cpu_online(unsigned int cpu)
|
|
{
|
|
struct device *dev;
|
|
|
|
dev = get_cpu_device(cpu);
|
|
microcode_update_cpu(cpu);
|
|
pr_debug("CPU%d added\n", cpu);
|
|
|
|
if (sysfs_create_group(&dev->kobj, &mc_attr_group))
|
|
pr_err("Failed to create group for CPU%d\n", cpu);
|
|
return 0;
|
|
}
|
|
|
|
static int mc_cpu_down_prep(unsigned int cpu)
|
|
{
|
|
struct device *dev;
|
|
|
|
dev = get_cpu_device(cpu);
|
|
/* Suspend is in progress, only remove the interface */
|
|
sysfs_remove_group(&dev->kobj, &mc_attr_group);
|
|
pr_debug("CPU%d removed\n", cpu);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct attribute *cpu_root_microcode_attrs[] = {
|
|
&dev_attr_reload.attr,
|
|
NULL
|
|
};
|
|
|
|
static const struct attribute_group cpu_root_microcode_group = {
|
|
.name = "microcode",
|
|
.attrs = cpu_root_microcode_attrs,
|
|
};
|
|
|
|
int __init microcode_init(void)
|
|
{
|
|
struct cpuinfo_x86 *c = &boot_cpu_data;
|
|
int error;
|
|
|
|
if (dis_ucode_ldr)
|
|
return -EINVAL;
|
|
|
|
if (c->x86_vendor == X86_VENDOR_INTEL)
|
|
microcode_ops = init_intel_microcode();
|
|
else if (c->x86_vendor == X86_VENDOR_AMD)
|
|
microcode_ops = init_amd_microcode();
|
|
else
|
|
pr_err("no support for this CPU vendor\n");
|
|
|
|
if (!microcode_ops)
|
|
return -ENODEV;
|
|
|
|
microcode_pdev = platform_device_register_simple("microcode", -1,
|
|
NULL, 0);
|
|
if (IS_ERR(microcode_pdev))
|
|
return PTR_ERR(microcode_pdev);
|
|
|
|
get_online_cpus();
|
|
mutex_lock(µcode_mutex);
|
|
|
|
error = subsys_interface_register(&mc_cpu_interface);
|
|
if (!error)
|
|
perf_check_microcode();
|
|
mutex_unlock(µcode_mutex);
|
|
put_online_cpus();
|
|
|
|
if (error)
|
|
goto out_pdev;
|
|
|
|
error = sysfs_create_group(&cpu_subsys.dev_root->kobj,
|
|
&cpu_root_microcode_group);
|
|
|
|
if (error) {
|
|
pr_err("Error creating microcode group!\n");
|
|
goto out_driver;
|
|
}
|
|
|
|
error = microcode_dev_init();
|
|
if (error)
|
|
goto out_ucode_group;
|
|
|
|
register_syscore_ops(&mc_syscore_ops);
|
|
cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/microcode:online",
|
|
mc_cpu_online, mc_cpu_down_prep);
|
|
|
|
pr_info("Microcode Update Driver: v%s.", DRIVER_VERSION);
|
|
|
|
return 0;
|
|
|
|
out_ucode_group:
|
|
sysfs_remove_group(&cpu_subsys.dev_root->kobj,
|
|
&cpu_root_microcode_group);
|
|
|
|
out_driver:
|
|
get_online_cpus();
|
|
mutex_lock(µcode_mutex);
|
|
|
|
subsys_interface_unregister(&mc_cpu_interface);
|
|
|
|
mutex_unlock(µcode_mutex);
|
|
put_online_cpus();
|
|
|
|
out_pdev:
|
|
platform_device_unregister(microcode_pdev);
|
|
return error;
|
|
|
|
}
|
|
fs_initcall(save_microcode_in_initrd);
|
|
late_initcall(microcode_init);
|