Introduce vfio-pci-core subsystem
Subsystems (6)
v4 cover Introduce vfio-pci-core subsystem
Hi Alex, Cornelia, Jason and Co,
This series split the vfio_pci driver into 2 parts: pci drivers and a subsystem driver that will also be library of code. The main pci driver, vfio_pci.ko will be used as before and it will bind to the subsystem driver vfio_pci_core.ko to register to the VFIO subsystem.
This series is coming to solve some of the issues that were raised in the previous attempts for extending vfio-pci for vendor specific functionality:
1. https://lkml.org/lkml/2020/5/17/376 by Yan Zhao.
2. https://www.spinics.net/lists/kernel/msg3903996.html by Longfang Liu
This subsystem framework will also ease on adding new vendor specific functionality to VFIO devices in the future by allowing another module to provide the pci_driver that can setup number of details before registering to VFIO subsystem (such as inject its own operations).
This series also extends the "driver_override" mechanism. We added a flag for pci drivers that will declare themselves as "driver_override" capable and only declared drivers can use this mechanism in the PCI subsystem. Other drivers will not be able to bind to devices that use "driver_override". Also, the PCI driver matching will always look for ID table and will never generate dummy "match_all" ID table in the PCI subsystem layer. In this way, we ensure deterministic behaviour with no races with the original pci drivers. In order to get the best match for "driver_override" drivers, one can create a userspace program (example can be found at
https://github.com/maxgurtovoy/linux_tools/blob/main/vfio/bind_vfio_pci_driver.py)
that find the 'best match' according to simple algorithm: "the driver with the fewest '*' matches wins."
For example, the vfio-pci driver will match to any pci device. So it will have the maximal '*' matches (for all matching IDs: vendor, device, subvendor, ...).
In case we are looking for a match to mlx5 based device, we'll have a match to vfio-pci.ko and mlx5-vfio-pci.ko. We'll prefer mlx5-vfio-pci.ko since it will have less '*' matches (probably vendor and device IDs will match). This will work in the future for NVMe/Virtio devices that can match according to a class code or other criteria.
The main goal of this series is to agree on the vfio_pci module split and the "driver_override" extensions. The follow-up version will include an extended mlx5_vfio_pci driver that will support VF suspend/resume as well.
This series applied cleanly on top of vfio reflck re-design (still haven't sent for review) and can be found at:
https://github.com/Mellanox/NVMEoF-P2P/tree/vfio-v4-external.
Max Gurtovoy (11):
vfio-pci: rename vfio_pci.c to vfio_pci_core.c
vfio-pci: rename vfio_pci_private.h to vfio_pci_core.h
vfio-pci: rename vfio_pci_device to vfio_pci_core_device
vfio-pci: rename ops functions to fit core namings
vfio-pci: include vfio header in vfio_pci_core.h
vfio-pci: introduce vfio_pci.c
vfio-pci: move igd initialization to vfio_pci.c
PCI: add flags field to pci_device_id structure
PCI: add matching checks for driver_override binding
vfio-pci: introduce vfio_pci_core subsystem driver
mlx5-vfio-pci: add new vfio_pci driver for mlx5 devices
Documentation/ABI/testing/sysfs-bus-pci | 6 +- Documentation/PCI/pci.rst | 1 + drivers/pci/pci-driver.c | 22 +- drivers/vfio/pci/Kconfig | 27 +- drivers/vfio/pci/Makefile | 12 +- drivers/vfio/pci/mlx5_vfio_pci.c | 130 + drivers/vfio/pci/vfio_pci.c | 2329 +---------------- drivers/vfio/pci/vfio_pci_config.c | 70 +- drivers/vfio/pci/vfio_pci_core.c | 2239 ++++++++++++++++ drivers/vfio/pci/vfio_pci_igd.c | 16 +- drivers/vfio/pci/vfio_pci_intrs.c | 42 +- drivers/vfio/pci/vfio_pci_rdwr.c | 18 +- drivers/vfio/pci/vfio_pci_zdev.c | 4 +- include/linux/mod_devicetable.h | 9 + include/linux/pci.h | 27 + .../linux/vfio_pci_core.h | 93 +- scripts/mod/devicetable-offsets.c | 1 + scripts/mod/file2alias.c | 8 +- 18 files changed, 2695 insertions(+), 2359 deletions(-) create mode 100644 drivers/vfio/pci/mlx5_vfio_pci.c create mode 100644 drivers/vfio/pci/vfio_pci_core.c rename drivers/vfio/pci/vfio_pci_private.h => include/linux/vfio_pci_core.h (56%)
Signature · 1 line
2.21.0
PATCH v4 1/11 vfio-pci: rename vfio_pci.c to vfio_pci_core.c
This is a preparation patch for separating the vfio_pci driver to a subsystem driver and a generic pci driver. This patch doesn't change any logic.
---
drivers/vfio/pci/Makefile | 2 +-
drivers/vfio/pci/{vfio_pci.c => vfio_pci_core.c} | 0
2 files changed, 1 insertion(+), 1 deletion(-)
rename drivers/vfio/pci/{vfio_pci.c => vfio_pci_core.c} (100%)drivers/vfio/pci/Makefile +1 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only -vfio-pci-y := vfio_pci.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o +vfio-pci-y := vfio_pci_core.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o vfio-pci-$(CONFIG_S390) += vfio_pci_zdev.o
drivers/vfio/pci/vfio_pci_core.c renamed from drivers/vfio/pci/vfio_pci.c +0 -0
Signature · 1 line
2.21.0
PATCH v4 2/11 vfio-pci: rename vfio_pci_private.h to vfio_pci_core.h
In reply to Max GurtovoyThis is a preparation patch for separating the vfio_pci driver to a subsystem driver and a generic pci driver. This patch doesn't change any logic.
---
drivers/vfio/pci/vfio_pci_config.c | 2 +-
drivers/vfio/pci/vfio_pci_core.c | 2 +-
drivers/vfio/pci/{vfio_pci_private.h => vfio_pci_core.h} | 6 +++---
drivers/vfio/pci/vfio_pci_igd.c | 2 +-
drivers/vfio/pci/vfio_pci_intrs.c | 2 +-
drivers/vfio/pci/vfio_pci_rdwr.c | 2 +-
drivers/vfio/pci/vfio_pci_zdev.c | 2 +-
7 files changed, 9 insertions(+), 9 deletions(-)
rename drivers/vfio/pci/{vfio_pci_private.h => vfio_pci_core.h} (98%)drivers/vfio/pci/vfio_pci_config.c +1 -1
@@ -26,7 +26,7 @@#include <linux/vfio.h> #include <linux/slab.h> -#include "vfio_pci_private.h" +#include "vfio_pci_core.h" /* Fake capability ID for standard config space */ #define PCI_CAP_ID_BASIC 0
drivers/vfio/pci/vfio_pci_core.c +1 -1
@@ -28,7 +28,7 @@#include <linux/nospec.h> #include <linux/sched/mm.h> -#include "vfio_pci_private.h" +#include "vfio_pci_core.h" #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"
drivers/vfio/pci/vfio_pci_core.h renamed from drivers/vfio/pci/vfio_pci_private.h +3 -3
@@ -15,8 +15,8 @@#include <linux/uuid.h> #include <linux/notifier.h> -#ifndef VFIO_PCI_PRIVATE_H -#define VFIO_PCI_PRIVATE_H +#ifndef VFIO_PCI_CORE_H +#define VFIO_PCI_CORE_H #define VFIO_PCI_OFFSET_SHIFT 40
@@ -205,4 +205,4 @@ static inline int vfio_pci_info_zdev_add_caps(struct vfio_pci_device *vdev, }#endif -#endif /* VFIO_PCI_PRIVATE_H */ +#endif /* VFIO_PCI_CORE_H */
drivers/vfio/pci/vfio_pci_igd.c +1 -1
@@ -15,7 +15,7 @@#include <linux/uaccess.h> #include <linux/vfio.h> -#include "vfio_pci_private.h" +#include "vfio_pci_core.h" #define OPREGION_SIGNATURE "IntelGraphicsMem"#define OPREGION_SIZE (8 * 1024)
drivers/vfio/pci/vfio_pci_intrs.c +1 -1
@@ -20,7 +20,7 @@#include <linux/wait.h> #include <linux/slab.h> -#include "vfio_pci_private.h" +#include "vfio_pci_core.h" /* * INTx
drivers/vfio/pci/vfio_pci_rdwr.c +1 -1
@@ -17,7 +17,7 @@#include <linux/vfio.h> #include <linux/vgaarb.h> -#include "vfio_pci_private.h" +#include "vfio_pci_core.h" #ifdef __LITTLE_ENDIAN#define vfio_ioread64 ioread64
drivers/vfio/pci/vfio_pci_zdev.c +1 -1
@@ -19,7 +19,7 @@#include <asm/pci_clp.h> #include <asm/pci_io.h> -#include "vfio_pci_private.h" +#include "vfio_pci_core.h" /* * Add the Base PCI Function information to the device info region.
Signature · 1 line
2.21.0
PATCH v4 3/11 vfio-pci: rename vfio_pci_device to vfio_pci_core_device
In reply to Max GurtovoyThis is a preparation patch for separating the vfio_pci driver to a subsystem driver and a generic pci driver. This patch doesn't change any logic. The new vfio_pci_core_device structure will be the main structure of the core driver and later on vfio_pci_device structure will be the main structure of the generic vfio_pci driver.
--- drivers/vfio/pci/vfio_pci_config.c | 68 ++++++++-------- drivers/vfio/pci/vfio_pci_core.c | 124 ++++++++++++++--------------- drivers/vfio/pci/vfio_pci_core.h | 52 ++++++------ drivers/vfio/pci/vfio_pci_igd.c | 14 ++-- drivers/vfio/pci/vfio_pci_intrs.c | 40 +++++----- drivers/vfio/pci/vfio_pci_rdwr.c | 16 ++-- drivers/vfio/pci/vfio_pci_zdev.c | 2 +- 7 files changed, 158 insertions(+), 158 deletions(-)
drivers/vfio/pci/vfio_pci_config.c +34 -34
@@ -108,9 +108,9 @@ static const u16 pci_ext_cap_length[PCI_EXT_CAP_ID_MAX + 1] = {struct perm_bits {u8 *virt;/* read/write virtual data, not hw */ u8 *write;/* writeable bits */ -int (*readfn)(struct vfio_pci_device *vdev,int pos,int count, +int (*readfn)(struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 *val); -int (*writefn)(struct vfio_pci_device *vdev,int pos,int count, +int (*writefn)(struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 val); };
@@ -171,7 +171,7 @@ static int vfio_user_config_write(struct pci_dev *pdev, int offset,return ret; } -static int vfio_default_config_read (struct vfio_pci_device *vdev,int pos, +static int vfio_default_config_read (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 *val) {
@@ -197,7 +197,7 @@ static int vfio_default_config_read(struct vfio_pci_device *vdev, int pos,return count; } -static int vfio_default_config_write (struct vfio_pci_device *vdev,int pos, +static int vfio_default_config_write (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 val) {
@@ -244,7 +244,7 @@ static int vfio_default_config_write(struct vfio_pci_device *vdev, int pos, }/* Allow direct read from hardware, except for capability next pointer */ -static int vfio_direct_config_read (struct vfio_pci_device *vdev,int pos, +static int vfio_direct_config_read (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 *val) {
@@ -269,7 +269,7 @@ static int vfio_direct_config_read(struct vfio_pci_device *vdev, int pos, }/* Raw access skips any kind of virtualization */ -static int vfio_raw_config_write (struct vfio_pci_device *vdev,int pos, +static int vfio_raw_config_write (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 val) {
@@ -282,7 +282,7 @@ static int vfio_raw_config_write(struct vfio_pci_device *vdev, int pos,return count; } -static int vfio_raw_config_read (struct vfio_pci_device *vdev,int pos, +static int vfio_raw_config_read (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 *val) {
@@ -296,7 +296,7 @@ static int vfio_raw_config_read(struct vfio_pci_device *vdev, int pos, }/* Virt access uses only virtualization */ -static int vfio_virt_config_write (struct vfio_pci_device *vdev,int pos, +static int vfio_virt_config_write (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 val) {
@@ -304,7 +304,7 @@ static int vfio_virt_config_write(struct vfio_pci_device *vdev, int pos,return count; } -static int vfio_virt_config_read (struct vfio_pci_device *vdev,int pos, +static int vfio_virt_config_read (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 *val) {
@@ -396,7 +396,7 @@ static inline void p_setd(struct perm_bits *p, int off, u32 virt, u32 write) }/* Caller should hold memory_lock semaphore */ -bool __vfio_pci_memory_enabled (struct vfio_pci_device *vdev) +bool __vfio_pci_memory_enabled (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;u16 cmd =le16_to_cpu (*(__le16 *)&vdev->vconfig[PCI_COMMAND ]);
@@ -413,7 +413,7 @@ bool __vfio_pci_memory_enabled(struct vfio_pci_device *vdev)* Restore the *real* BARs after we detect a FLR or backdoor reset. * (backdoor = some device specific technique that we didn't catch) */ -static void vfio_bar_restore (struct vfio_pci_device *vdev) +static void vfio_bar_restore (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;u32 *rbar = vdev->rbar;
@@ -460,7 +460,7 @@ static __le32 vfio_generate_bar_flags(struct pci_dev *pdev, int bar)* Pretend we're hardware and tweak the values of the *virtual* PCI BARs * to reflect the hardware capabilities. This implements BAR sizing. */ -static void vfio_bar_fixup (struct vfio_pci_device *vdev) +static void vfio_bar_fixup (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;int i;
@@ -514,7 +514,7 @@ static void vfio_bar_fixup(struct vfio_pci_device *vdev) vdev->bardirty = false; } -static int vfio_basic_config_read (struct vfio_pci_device *vdev,int pos, +static int vfio_basic_config_read (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 *val) {
@@ -536,7 +536,7 @@ static int vfio_basic_config_read(struct vfio_pci_device *vdev, int pos, }/* Test whether BARs match the value we think they should contain */ -static bool vfio_need_bar_restore (struct vfio_pci_device *vdev) +static bool vfio_need_bar_restore (struct vfio_pci_core_device *vdev) {int i =0 , pos =PCI_BASE_ADDRESS_0 , ret;u32 bar;
@@ -552,7 +552,7 @@ static bool vfio_need_bar_restore(struct vfio_pci_device *vdev)return false; } -static int vfio_basic_config_write (struct vfio_pci_device *vdev,int pos, +static int vfio_basic_config_write (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 val) {
@@ -692,7 +692,7 @@ static int __init init_pci_cap_basic_perm(struct perm_bits *perm)return 0 ; } -static int vfio_pm_config_write (struct vfio_pci_device *vdev,int pos, +static int vfio_pm_config_write (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 val) {
@@ -747,7 +747,7 @@ static int __init init_pci_cap_pm_perm(struct perm_bits *perm)return 0 ; } -static int vfio_vpd_config_write (struct vfio_pci_device *vdev,int pos, +static int vfio_vpd_config_write (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 val) {
@@ -829,7 +829,7 @@ static int __init init_pci_cap_pcix_perm(struct perm_bits *perm)return 0 ; } -static int vfio_exp_config_write (struct vfio_pci_device *vdev,int pos, +static int vfio_exp_config_write (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 val) {
@@ -913,7 +913,7 @@ static int __init init_pci_cap_exp_perm(struct perm_bits *perm)return 0 ; } -static int vfio_af_config_write (struct vfio_pci_device *vdev,int pos, +static int vfio_af_config_write (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 val) {
@@ -1072,7 +1072,7 @@ int __init vfio_pci_init_perm_bits(void)return ret; } -static int vfio_find_cap_start (struct vfio_pci_device *vdev,int pos) +static int vfio_find_cap_start (struct vfio_pci_core_device *vdev,int pos) {u8 cap; int base = (pos >= PCI_CFG_SPACE_SIZE) ? PCI_CFG_SPACE_SIZE :
@@ -1089,7 +1089,7 @@ static int vfio_find_cap_start(struct vfio_pci_device *vdev, int pos)return pos; } -static int vfio_msi_config_read (struct vfio_pci_device *vdev,int pos, +static int vfio_msi_config_read (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 *val) {
@@ -1109,7 +1109,7 @@ static int vfio_msi_config_read(struct vfio_pci_device *vdev, int pos,return vfio_default_config_read (vdev, pos, count, perm, offset, val); } -static int vfio_msi_config_write (struct vfio_pci_device *vdev,int pos, +static int vfio_msi_config_write (struct vfio_pci_core_device *vdev,int pos,int count,struct perm_bits *perm,int offset,__le32 val) {
@@ -1189,7 +1189,7 @@ static int init_pci_cap_msi_perm(struct perm_bits *perm, int len, u16 flags) }/* Determine MSI CAP field length; initialize msi_perms on 1st call per vdev */ -static int vfio_msi_cap_len (struct vfio_pci_device *vdev,u8 pos) +static int vfio_msi_cap_len (struct vfio_pci_core_device *vdev,u8 pos) {struct pci_dev *pdev = vdev->pdev;int len, ret;
@@ -1222,7 +1222,7 @@ static int vfio_msi_cap_len(struct vfio_pci_device *vdev, u8 pos) }/* Determine extended capability length for VC (2 & 9) and MFVC */ -static int vfio_vc_cap_len (struct vfio_pci_device *vdev,u16 pos) +static int vfio_vc_cap_len (struct vfio_pci_core_device *vdev,u16 pos) {struct pci_dev *pdev = vdev->pdev;u32 tmp;
@@ -1263,7 +1263,7 @@ static int vfio_vc_cap_len(struct vfio_pci_device *vdev, u16 pos)return len; } -static int vfio_cap_len (struct vfio_pci_device *vdev,u8 cap,u8 pos) +static int vfio_cap_len (struct vfio_pci_core_device *vdev,u8 cap,u8 pos) {struct pci_dev *pdev = vdev->pdev;u32 dword;
@@ -1338,7 +1338,7 @@ static int vfio_cap_len(struct vfio_pci_device *vdev, u8 cap, u8 pos)return 0 ; } -static int vfio_ext_cap_len (struct vfio_pci_device *vdev,u16 ecap,u16 epos) +static int vfio_ext_cap_len (struct vfio_pci_core_device *vdev,u16 ecap,u16 epos) {struct pci_dev *pdev = vdev->pdev;u8 byte;
@@ -1412,7 +1412,7 @@ static int vfio_ext_cap_len(struct vfio_pci_device *vdev, u16 ecap, u16 epos)return 0 ; } -static int vfio_fill_vconfig_bytes (struct vfio_pci_device *vdev, +static int vfio_fill_vconfig_bytes (struct vfio_pci_core_device *vdev,int offset,int size) {struct pci_dev *pdev = vdev->pdev;
@@ -1459,7 +1459,7 @@ static int vfio_fill_vconfig_bytes(struct vfio_pci_device *vdev,return ret; } -static int vfio_cap_init (struct vfio_pci_device *vdev) +static int vfio_cap_init (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;u8 *map = vdev->pci_config_map;
@@ -1549,7 +1549,7 @@ static int vfio_cap_init(struct vfio_pci_device *vdev)return 0 ; } -static int vfio_ecap_init (struct vfio_pci_device *vdev) +static int vfio_ecap_init (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;u8 *map = vdev->pci_config_map;
@@ -1669,7 +1669,7 @@ static const struct pci_device_id known_bogus_vf_intx_pin[] = {* for each area requiring emulated bits, but the array of pointers * would be comparable in size (at least for standard config space). */ -int vfio_config_init (struct vfio_pci_device *vdev) +int vfio_config_init (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;u8 *map, *vconfig;
@@ -1773,7 +1773,7 @@ int vfio_config_init(struct vfio_pci_device *vdev)return pcibios_err_to_errno (ret); } -void vfio_config_free (struct vfio_pci_device *vdev) +void vfio_config_free (struct vfio_pci_core_device *vdev) {kfree (vdev->vconfig); vdev->vconfig =NULL ;
@@ -1790,7 +1790,7 @@ void vfio_config_free(struct vfio_pci_device *vdev)* Find the remaining number of bytes in a dword that match the given * position. Stop at either the end of the capability or the dword boundary. */ -static size_t vfio_pci_cap_remaining_dword (struct vfio_pci_device *vdev, +static size_t vfio_pci_cap_remaining_dword (struct vfio_pci_core_device *vdev,loff_t pos) {u8 cap = vdev->pci_config_map[pos];
@@ -1802,7 +1802,7 @@ static size_t vfio_pci_cap_remaining_dword(struct vfio_pci_device *vdev,return i; } -static ssize_t vfio_config_do_rw (struct vfio_pci_device *vdev,char __user *buf, +static ssize_t vfio_config_do_rw (struct vfio_pci_core_device *vdev,char __user *buf,size_t count,loff_t *ppos,bool iswrite) {struct pci_dev *pdev = vdev->pdev;
@@ -1885,7 +1885,7 @@ static ssize_t vfio_config_do_rw(struct vfio_pci_device *vdev, char __user *buf,return ret; } -ssize_t vfio_pci_config_rw (struct vfio_pci_device *vdev,char __user *buf, +ssize_t vfio_pci_config_rw (struct vfio_pci_core_device *vdev,char __user *buf,size_t count,loff_t *ppos,bool iswrite) {size_t done =0 ;
drivers/vfio/pci/vfio_pci_core.c +62 -62
419 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_core.h +26 -26
119 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_igd.c +7 -7
@@ -25,7 +25,7 @@#define OPREGION_RVDS 0x3c2#define OPREGION_VERSION 0x16 -static size_t vfio_pci_igd_rw (struct vfio_pci_device *vdev,char __user *buf, +static size_t vfio_pci_igd_rw (struct vfio_pci_core_device *vdev,char __user *buf,size_t count,loff_t *ppos,bool iswrite) {unsigned int i =VFIO_PCI_OFFSET_TO_INDEX (*ppos) -VFIO_PCI_NUM_REGIONS ;
@@ -45,7 +45,7 @@ static size_t vfio_pci_igd_rw(struct vfio_pci_device *vdev, char __user *buf,return count; } -static void vfio_pci_igd_release (struct vfio_pci_device *vdev, +static void vfio_pci_igd_release (struct vfio_pci_core_device *vdev,struct vfio_pci_region *region) {memunmap (region->data);
@@ -56,7 +56,7 @@ static const struct vfio_pci_regops vfio_pci_igd_regops = { .release = vfio_pci_igd_release, }; -static int vfio_pci_igd_opregion_init (struct vfio_pci_device *vdev) +static int vfio_pci_igd_opregion_init (struct vfio_pci_core_device *vdev) {__le32 *dwordp = (__le32 *)(vdev->vconfig +OPREGION_PCI_ADDR );u32 addr, size;
@@ -160,7 +160,7 @@ static int vfio_pci_igd_opregion_init(struct vfio_pci_device *vdev)return ret; } -static size_t vfio_pci_igd_cfg_rw (struct vfio_pci_device *vdev, +static size_t vfio_pci_igd_cfg_rw (struct vfio_pci_core_device *vdev,char __user *buf,size_t count,loff_t *ppos,bool iswrite) {
@@ -253,7 +253,7 @@ static size_t vfio_pci_igd_cfg_rw(struct vfio_pci_device *vdev,return count; } -static void vfio_pci_igd_cfg_release (struct vfio_pci_device *vdev, +static void vfio_pci_igd_cfg_release (struct vfio_pci_core_device *vdev,struct vfio_pci_region *region) {struct pci_dev *pdev = region->data;
@@ -266,7 +266,7 @@ static const struct vfio_pci_regops vfio_pci_igd_cfg_regops = { .release = vfio_pci_igd_cfg_release, }; -static int vfio_pci_igd_cfg_init (struct vfio_pci_device *vdev) +static int vfio_pci_igd_cfg_init (struct vfio_pci_core_device *vdev) {struct pci_dev *host_bridge, *lpc_bridge;int ret;
@@ -314,7 +314,7 @@ static int vfio_pci_igd_cfg_init(struct vfio_pci_device *vdev)return 0 ; } -int vfio_pci_igd_init (struct vfio_pci_device *vdev) +int vfio_pci_igd_init (struct vfio_pci_core_device *vdev) {int ret;
drivers/vfio/pci/vfio_pci_intrs.c +20 -20
@@ -27,13 +27,13 @@*/ static void vfio_send_intx_eventfd (void *opaque,void *unused) { -struct vfio_pci_device *vdev = opaque; +struct vfio_pci_core_device *vdev = opaque;if (likely (is_intx (vdev) && !vdev->virq_disabled))eventfd_signal (vdev->ctx[0 ].trigger,1 ); } -void vfio_pci_intx_mask (struct vfio_pci_device *vdev) +void vfio_pci_intx_mask (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;unsigned long flags;
@@ -73,7 +73,7 @@ void vfio_pci_intx_mask(struct vfio_pci_device *vdev)*/ static int vfio_pci_intx_unmask_handler (void *opaque,void *unused) { -struct vfio_pci_device *vdev = opaque; +struct vfio_pci_core_device *vdev = opaque;struct pci_dev *pdev = vdev->pdev;unsigned long flags;int ret =0 ;
@@ -107,7 +107,7 @@ static int vfio_pci_intx_unmask_handler(void *opaque, void *unused)return ret; } -void vfio_pci_intx_unmask (struct vfio_pci_device *vdev) +void vfio_pci_intx_unmask (struct vfio_pci_core_device *vdev) {if (vfio_pci_intx_unmask_handler (vdev,NULL ) >0 )vfio_send_intx_eventfd (vdev,NULL );
@@ -115,7 +115,7 @@ void vfio_pci_intx_unmask(struct vfio_pci_device *vdev)static irqreturn_t vfio_intx_handler (int irq,void *dev_id) { -struct vfio_pci_device *vdev = dev_id; +struct vfio_pci_core_device *vdev = dev_id;unsigned long flags;int ret =IRQ_NONE ;
@@ -139,7 +139,7 @@ static irqreturn_t vfio_intx_handler(int irq, void *dev_id)return ret; } -static int vfio_intx_enable (struct vfio_pci_device *vdev) +static int vfio_intx_enable (struct vfio_pci_core_device *vdev) {if (!is_irq_none (vdev))return -EINVAL ;
@@ -168,7 +168,7 @@ static int vfio_intx_enable(struct vfio_pci_device *vdev)return 0 ; } -static int vfio_intx_set_signal (struct vfio_pci_device *vdev,int fd) +static int vfio_intx_set_signal (struct vfio_pci_core_device *vdev,int fd) {struct pci_dev *pdev = vdev->pdev;unsigned long irqflags =IRQF_SHARED ;
@@ -223,7 +223,7 @@ static int vfio_intx_set_signal(struct vfio_pci_device *vdev, int fd)return 0 ; } -static void vfio_intx_disable (struct vfio_pci_device *vdev) +static void vfio_intx_disable (struct vfio_pci_core_device *vdev) {vfio_virqfd_disable (&vdev->ctx[0 ].unmask);vfio_virqfd_disable (&vdev->ctx[0 ].mask);
@@ -244,7 +244,7 @@ static irqreturn_t vfio_msihandler(int irq, void *arg)return IRQ_HANDLED ; } -static int vfio_msi_enable (struct vfio_pci_device *vdev,int nvec,bool msix) +static int vfio_msi_enable (struct vfio_pci_core_device *vdev,int nvec,bool msix) {struct pci_dev *pdev = vdev->pdev;unsigned int flag = msix ?PCI_IRQ_MSIX :PCI_IRQ_MSI ;
@@ -285,7 +285,7 @@ static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)return 0 ; } -static int vfio_msi_set_vector_signal (struct vfio_pci_device *vdev, +static int vfio_msi_set_vector_signal (struct vfio_pci_core_device *vdev,int vector,int fd,bool msix) {struct pci_dev *pdev = vdev->pdev;
@@ -364,7 +364,7 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,return 0 ; } -static int vfio_msi_set_block (struct vfio_pci_device *vdev,unsigned start, +static int vfio_msi_set_block (struct vfio_pci_core_device *vdev,unsigned start,unsigned count,int32_t *fds,bool msix) {int i, j, ret =0 ;
@@ -385,7 +385,7 @@ static int vfio_msi_set_block(struct vfio_pci_device *vdev, unsigned start,return ret; } -static void vfio_msi_disable (struct vfio_pci_device *vdev,bool msix) +static void vfio_msi_disable (struct vfio_pci_core_device *vdev,bool msix) {struct pci_dev *pdev = vdev->pdev;int i;
@@ -417,7 +417,7 @@ static void vfio_msi_disable(struct vfio_pci_device *vdev, bool msix)/* * IOCTL support */ -static int vfio_pci_set_intx_unmask (struct vfio_pci_device *vdev, +static int vfio_pci_set_intx_unmask (struct vfio_pci_core_device *vdev,unsigned index,unsigned start,unsigned count,uint32_t flags,void *data) {
@@ -444,7 +444,7 @@ static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev,return 0 ; } -static int vfio_pci_set_intx_mask (struct vfio_pci_device *vdev, +static int vfio_pci_set_intx_mask (struct vfio_pci_core_device *vdev,unsigned index,unsigned start,unsigned count,uint32_t flags,void *data) {
@@ -464,7 +464,7 @@ static int vfio_pci_set_intx_mask(struct vfio_pci_device *vdev,return 0 ; } -static int vfio_pci_set_intx_trigger (struct vfio_pci_device *vdev, +static int vfio_pci_set_intx_trigger (struct vfio_pci_core_device *vdev,unsigned index,unsigned start,unsigned count,uint32_t flags,void *data) {
@@ -507,7 +507,7 @@ static int vfio_pci_set_intx_trigger(struct vfio_pci_device *vdev,return 0 ; } -static int vfio_pci_set_msi_trigger (struct vfio_pci_device *vdev, +static int vfio_pci_set_msi_trigger (struct vfio_pci_core_device *vdev,unsigned index,unsigned start,unsigned count,uint32_t flags,void *data) {
@@ -613,7 +613,7 @@ static int vfio_pci_set_ctx_trigger_single(struct eventfd_ctx **ctx,return -EINVAL ; } -static int vfio_pci_set_err_trigger (struct vfio_pci_device *vdev, +static int vfio_pci_set_err_trigger (struct vfio_pci_core_device *vdev,unsigned index,unsigned start,unsigned count,uint32_t flags,void *data) {
@@ -624,7 +624,7 @@ static int vfio_pci_set_err_trigger(struct vfio_pci_device *vdev, count, flags, data); } -static int vfio_pci_set_req_trigger (struct vfio_pci_device *vdev, +static int vfio_pci_set_req_trigger (struct vfio_pci_core_device *vdev,unsigned index,unsigned start,unsigned count,uint32_t flags,void *data) {
@@ -635,11 +635,11 @@ static int vfio_pci_set_req_trigger(struct vfio_pci_device *vdev, count, flags, data); } -int vfio_pci_set_irqs_ioctl (struct vfio_pci_device *vdev,uint32_t flags, +int vfio_pci_set_irqs_ioctl (struct vfio_pci_core_device *vdev,uint32_t flags,unsigned index,unsigned start,unsigned count,void *data) { -int (*func)(struct vfio_pci_device *vdev,unsigned index, +int (*func)(struct vfio_pci_core_device *vdev,unsigned index,unsigned start,unsigned count,uint32_t flags,void *data) =NULL ;
drivers/vfio/pci/vfio_pci_rdwr.c +8 -8
@@ -38,7 +38,7 @@#define vfio_iowrite8 iowrite8#define VFIO_IOWRITE (size) \ -static int vfio_pci_iowrite##size(struct vfio_pci_device *vdev, \ +static int vfio_pci_iowrite##size(struct vfio_pci_core_device *vdev, \ bool test_mem, u##size val, void __iomem *io) \ { \ if (test_mem) { \
@@ -65,7 +65,7 @@ VFIO_IOWRITE(64)#endif #define VFIO_IOREAD (size) \ -static int vfio_pci_ioread##size(struct vfio_pci_device *vdev, \ +static int vfio_pci_ioread##size(struct vfio_pci_core_device *vdev, \ bool test_mem, u##size *val, void __iomem *io) \ { \ if (test_mem) { \
@@ -94,7 +94,7 @@ VFIO_IOREAD(32)* reads with -1. This is intended for handling MSI-X vector tables and * leftover space for ROM BARs. */ -static ssize_t do_io_rw (struct vfio_pci_device *vdev,bool test_mem, +static ssize_t do_io_rw (struct vfio_pci_core_device *vdev,bool test_mem,void __iomem *io,char __user *buf,loff_t off,size_t count,size_t x_start,size_t x_end,bool iswrite)
@@ -200,7 +200,7 @@ static ssize_t do_io_rw(struct vfio_pci_device *vdev, bool test_mem,return done; } -static int vfio_pci_setup_barmap (struct vfio_pci_device *vdev,int bar) +static int vfio_pci_setup_barmap (struct vfio_pci_core_device *vdev,int bar) {struct pci_dev *pdev = vdev->pdev;int ret;
@@ -224,7 +224,7 @@ static int vfio_pci_setup_barmap(struct vfio_pci_device *vdev, int bar)return 0 ; } -ssize_t vfio_pci_bar_rw (struct vfio_pci_device *vdev,char __user *buf, +ssize_t vfio_pci_bar_rw (struct vfio_pci_core_device *vdev,char __user *buf,size_t count,loff_t *ppos,bool iswrite) {struct pci_dev *pdev = vdev->pdev;
@@ -288,7 +288,7 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,return done; } -ssize_t vfio_pci_vga_rw (struct vfio_pci_device *vdev,char __user *buf, +ssize_t vfio_pci_vga_rw (struct vfio_pci_core_device *vdev,char __user *buf,size_t count,loff_t *ppos,bool iswrite) {int ret;
@@ -384,7 +384,7 @@ static void vfio_pci_ioeventfd_do_write(struct vfio_pci_ioeventfd *ioeventfd,static int vfio_pci_ioeventfd_handler (void *opaque,void *unused) {struct vfio_pci_ioeventfd *ioeventfd = opaque; -struct vfio_pci_device *vdev = ioeventfd->vdev; +struct vfio_pci_core_device *vdev = ioeventfd->vdev;if (ioeventfd->test_mem) {if (!down_read_trylock (&vdev->memory_lock))
@@ -410,7 +410,7 @@ static void vfio_pci_ioeventfd_thread(void *opaque, void *unused)vfio_pci_ioeventfd_do_write (ioeventfd, ioeventfd->test_mem); } -long vfio_pci_ioeventfd (struct vfio_pci_device *vdev,loff_t offset, +long vfio_pci_ioeventfd (struct vfio_pci_core_device *vdev,loff_t offset,uint64_t data,int count,int fd) {struct pci_dev *pdev = vdev->pdev;
drivers/vfio/pci/vfio_pci_zdev.c +1 -1
@@ -114,7 +114,7 @@ static int zpci_pfip_cap(struct zpci_dev *zdev, struct vfio_info_cap *caps)/* * Add all supported capabilities to the VFIO_DEVICE_GET_INFO capability chain. */ -int vfio_pci_info_zdev_add_caps (struct vfio_pci_device *vdev, +int vfio_pci_info_zdev_add_caps (struct vfio_pci_core_device *vdev,struct vfio_info_cap *caps) {struct zpci_dev *zdev =to_zpci (vdev->pdev);
Signature · 1 line
2.21.0
PATCH v4 4/11 vfio-pci: rename ops functions to fit core namings
In reply to Max GurtovoyThis is another preparation patch for separating the vfio_pci driver to a subsystem driver and a generic pci driver. This patch doesn't change any logic.
--- drivers/vfio/pci/vfio_pci_core.c | 36 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +18 -18
@@ -533,7 +533,7 @@ static void vfio_pci_vf_token_user_add(struct vfio_pci_core_device *vdev, int vavfio_device_put (&pf_vdev->vdev); } -static void vfio_pci_release (struct vfio_device *core_vdev) +static void vfio_pci_core_release (struct vfio_device *core_vdev) {struct vfio_pci_core_device *vdev = container_of(core_vdev, struct vfio_pci_core_device, vdev);
@@ -556,7 +556,7 @@ static void vfio_pci_release(struct vfio_device *core_vdev)mutex_unlock (&vdev->igate); } -static int vfio_pci_open (struct vfio_device *core_vdev) +static int vfio_pci_core_open (struct vfio_device *core_vdev) {struct vfio_pci_core_device *vdev = container_of(core_vdev, struct vfio_pci_core_device, vdev);
@@ -763,7 +763,7 @@ struct vfio_devices {int max_index; }; -static long vfio_pci_ioctl (struct vfio_device *core_vdev, +static long vfio_pci_core_ioctl (struct vfio_device *core_vdev,unsigned int cmd,unsigned long arg) { struct vfio_pci_core_device *vdev =
@@ -1394,7 +1394,7 @@ static ssize_t vfio_pci_rw(struct vfio_pci_core_device *vdev, char __user *buf,return -EINVAL ; } -static ssize_t vfio_pci_read (struct vfio_device *core_vdev,char __user *buf, +static ssize_t vfio_pci_core_read (struct vfio_device *core_vdev,char __user *buf,size_t count,loff_t *ppos) { struct vfio_pci_core_device *vdev =
@@ -1406,7 +1406,7 @@ static ssize_t vfio_pci_read(struct vfio_device *core_vdev, char __user *buf,return vfio_pci_rw (vdev, buf, count, ppos, false); } -static ssize_t vfio_pci_write (struct vfio_device *core_vdev,const char __user *buf, +static ssize_t vfio_pci_core_write (struct vfio_device *core_vdev,const char __user *buf,size_t count,loff_t *ppos) { struct vfio_pci_core_device *vdev =
@@ -1612,7 +1612,7 @@ static const struct vm_operations_struct vfio_pci_mmap_ops = { .fault = vfio_pci_mmap_fault, }; -static int vfio_pci_mmap (struct vfio_device *core_vdev,struct vm_area_struct *vma) +static int vfio_pci_core_mmap (struct vfio_device *core_vdev,struct vm_area_struct *vma) {struct vfio_pci_core_device *vdev = container_of(core_vdev, struct vfio_pci_core_device, vdev);
@@ -1683,7 +1683,7 @@ static int vfio_pci_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vreturn 0 ; } -static void vfio_pci_request (struct vfio_device *core_vdev,unsigned int count) +static void vfio_pci_core_request (struct vfio_device *core_vdev,unsigned int count) {struct vfio_pci_core_device *vdev = container_of(core_vdev, struct vfio_pci_core_device, vdev);
@@ -1799,7 +1799,7 @@ static int vfio_pci_validate_vf_token(struct vfio_pci_core_device *vdev,#define VF_TOKEN_ARG "vf_token=" -static int vfio_pci_match (struct vfio_device *core_vdev,char *buf) +static int vfio_pci_core_match (struct vfio_device *core_vdev,char *buf) {struct vfio_pci_core_device *vdev = container_of(core_vdev, struct vfio_pci_core_device, vdev);
@@ -1874,7 +1874,7 @@ static int vfio_pci_reflck_find(struct pci_dev *pdev, void *data)return 0 ; } -static int vfio_pci_reflck_attach (struct vfio_device *core_vdev) +static int vfio_pci_core_reflck_attach (struct vfio_device *core_vdev) {struct vfio_pci_core_device *vdev = container_of(core_vdev, struct vfio_pci_core_device, vdev);
@@ -1890,15 +1890,15 @@ static int vfio_pci_reflck_attach(struct vfio_device *core_vdev)static const struct vfio_device_ops vfio_pci_ops = { .name ="vfio-pci" , - .open = vfio_pci_open, - .release = vfio_pci_release, - .ioctl = vfio_pci_ioctl, - .read = vfio_pci_read, - .write = vfio_pci_write, - .mmap = vfio_pci_mmap, - .request = vfio_pci_request, - .match = vfio_pci_match, - .reflck_attach = vfio_pci_reflck_attach, + .open = vfio_pci_core_open, + .release = vfio_pci_core_release, + .ioctl = vfio_pci_core_ioctl, + .read = vfio_pci_core_read, + .write = vfio_pci_core_write, + .mmap = vfio_pci_core_mmap, + .request = vfio_pci_core_request, + .match = vfio_pci_core_match, + .reflck_attach = vfio_pci_core_reflck_attach, }; static int vfio_pci_bus_notifier(struct notifier_block *nb,
Signature · 1 line
2.21.0
PATCH v4 5/11 vfio-pci: include vfio header in vfio_pci_core.h
In reply to Max GurtovoyThe vfio_device structure is embedded into the vfio_pci_core_device structure, so there is no reason for not including the header file in the vfio_pci_core header as well.
--- drivers/vfio/pci/vfio_pci_core.c | 1 - drivers/vfio/pci/vfio_pci_core.h | 1 + 2 files changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/pci/vfio_pci_core.c +0 -1
@@ -23,7 +23,6 @@#include <linux/slab.h> #include <linux/types.h> #include <linux/uaccess.h> -#include <linux/vfio.h> #include <linux/vgaarb.h> #include <linux/nospec.h> #include <linux/sched/mm.h>
drivers/vfio/pci/vfio_pci_core.h +1 -0
@@ -10,6 +10,7 @@#include <linux/mutex.h> #include <linux/pci.h> +#include <linux/vfio.h> #include <linux/irqbypass.h> #include <linux/types.h> #include <linux/uuid.h>
Signature · 1 line
2.21.0
PATCH v4 6/11 vfio-pci: introduce vfio_pci.c
In reply to Max GurtovoySplit the vfio_pci driver into two logical parts, the 'struct pci_driver' (vfio_pci.c) and a library of code (vfio_pci_core.c) that helps creating a VFIO device on top of a PCI device.
As before vfio_pci.ko continues to present the same interface under sysfs and this change should have no functional impact.
vfio_pci_core.c exposes an interface that is similar to a typical Linux subsystem (and in the future it will become a separate module), in that a pci_driver doing probe() can setup a number of details and then register and create the VFIO char device.
This is a preparation for allowing another module to provide the pci_driver and allow that module to customize how VFIO is setup, inject its own operations, and easily extend vendor specific functionality.
--- drivers/vfio/pci/Makefile | 2 +- drivers/vfio/pci/vfio_pci.c | 227 ++++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_core.c | 234 +++++-------------------------- drivers/vfio/pci/vfio_pci_core.h | 22 +++ 4 files changed, 286 insertions(+), 199 deletions(-) create mode 100644 drivers/vfio/pci/vfio_pci.c
drivers/vfio/pci/Makefile +1 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only -vfio-pci-y := vfio_pci_core.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o +vfio-pci-y := vfio_pci.o vfio_pci_core.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o vfio-pci-$(CONFIG_S390) += vfio_pci_zdev.o
drivers/vfio/pci/vfio_pci.c new file +227 -0
227 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_core.c +36 -198
444 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_core.h +22 -0
@@ -206,4 +206,26 @@ static inline int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev, }#endif +/* Will be exported for vfio pci drivers usage */ +void vfio_pci_core_cleanup (void ); +int vfio_pci_core_init (void ); +void vfio_pci_core_release (struct vfio_device *core_vdev); +int vfio_pci_core_open (struct vfio_device *core_vdev); +int vfio_pci_core_register_device (struct vfio_pci_core_device *vdev, +struct pci_dev *pdev, +const struct vfio_device_ops *vfio_pci_ops); +void vfio_pci_core_unregister_device (struct vfio_pci_core_device *vdev); +int vfio_pci_core_sriov_configure (struct pci_dev *pdev,int nr_virtfn); +extern const struct pci_error_handlers vfio_pci_core_err_handlers; +long vfio_pci_core_ioctl (struct vfio_device *core_vdev,unsigned int cmd, +unsigned long arg); +ssize_t vfio_pci_core_read (struct vfio_device *core_vdev,char __user *buf, +size_t count,loff_t *ppos); +ssize_t vfio_pci_core_write (struct vfio_device *core_vdev,const char __user *buf, +size_t count,loff_t *ppos); +int vfio_pci_core_mmap (struct vfio_device *core_vdev,struct vm_area_struct *vma); +void vfio_pci_core_request (struct vfio_device *core_vdev,unsigned int count); +int vfio_pci_core_match (struct vfio_device *core_vdev,char *buf); +int vfio_pci_core_reflck_attach (struct vfio_device *core_vdev); +#endif /* VFIO_PCI_CORE_H */
Signature · 1 line
2.21.0
PATCH v4 7/11 vfio-pci: move igd initialization to vfio_pci.c
In reply to Max GurtovoyThis is a preparation before splitting vfio_pci.ko to 2 drivers. Move the vendor specific igd initialization from the core part to pci_driver part.
--- drivers/vfio/pci/vfio_pci.c | 31 ++++++++++++++++++++- drivers/vfio/pci/vfio_pci_core.c | 46 +++++++++++--------------------- drivers/vfio/pci/vfio_pci_core.h | 8 ++++++ 3 files changed, 54 insertions(+), 31 deletions(-)
drivers/vfio/pci/vfio_pci.c +30 -1
@@ -83,9 +83,38 @@ static bool vfio_pci_is_denylisted(struct pci_dev *pdev)return true; } +static int vfio_pci_open (struct vfio_device *core_vdev) +{ +struct vfio_pci_core_device *vdev = + container_of(core_vdev, struct vfio_pci_core_device, vdev); +struct pci_dev *pdev = vdev->pdev; +int ret; + +lockdep_assert_held (&core_vdev->reflck->lock); + + ret =vfio_pci_core_enable (vdev); +if (ret) +return ret; + +if (vfio_pci_is_vga (pdev) && + pdev->vendor ==PCI_VENDOR_ID_INTEL && +IS_ENABLED (CONFIG_VFIO_PCI_IGD )) { + ret =vfio_pci_igd_init (vdev); +if (ret && ret != -ENODEV ) { +pci_warn (pdev,"Failed to setup Intel IGD regions\n" ); +vfio_pci_core_disable (vdev); +return ret; + } + } + +vfio_pci_core_finish_enable (vdev); + +return 0 ; +} +static const struct vfio_device_ops vfio_pci_ops = { .name ="vfio-pci" , - .open = vfio_pci_core_open, + .open = vfio_pci_open, .release = vfio_pci_core_release, .ioctl = vfio_pci_core_ioctl, .read = vfio_pci_core_read,
drivers/vfio/pci/vfio_pci_core.c +16 -30
93 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_core.h +8 -0
@@ -227,5 +227,13 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vmavoid vfio_pci_core_request (struct vfio_device *core_vdev,unsigned int count);int vfio_pci_core_match (struct vfio_device *core_vdev,char *buf);int vfio_pci_core_reflck_attach (struct vfio_device *core_vdev); +int vfio_pci_core_enable (struct vfio_pci_core_device *vdev); +void vfio_pci_core_disable (struct vfio_pci_core_device *vdev); +void vfio_pci_core_finish_enable (struct vfio_pci_core_device *vdev); + +static inline bool vfio_pci_is_vga (struct pci_dev *pdev) +{ +return (pdev->class >>8 ) ==PCI_CLASS_DISPLAY_VGA ; +}#endif /* VFIO_PCI_CORE_H */
Signature · 1 line
2.21.0
PATCH v4 8/11 PCI: add flags field to pci_device_id structure
In reply to Max GurtovoyThis field will be used to allow pci modules to set some specific data that can be used for matching, aliases and other hints. Add example for "driver_override" pci drivers that will set a special prefix in the modules.alias table. In the future, this flag will enforce "driver_override" to work on drivers that specifically opt into this feature. The udev utility will not try to load these drivers automatically in order to bind to new discovered devices. This will be because the modalias tables populated by those drivers will be different from "regular" pci modalias tables. Userspace utilities, such as libvirt, will need to adjust and bind devices according to new matching mechanism with taking "driver_override" enforcement into consideration.
--- Documentation/PCI/pci.rst | 1 + include/linux/mod_devicetable.h | 9 +++++++++ include/linux/pci.h | 27 +++++++++++++++++++++++++++ scripts/mod/devicetable-offsets.c | 1 + scripts/mod/file2alias.c | 8 ++++++-- 5 files changed, 44 insertions(+), 2 deletions(-)
Documentation/PCI/pci.rst +1 -0
@@ -103,6 +103,7 @@ need pass only as many optional fields as necessary: - subvendor and subdevice fields default to PCI_ANY_ID (FFFFFFFF) - class and classmask fields default to 0 - driver_data defaults to 0UL. + - flags field defaults to 0. Note that driver_data must match the value used by any of the pci_device_id entries defined in the driver. This makes the driver_data field mandatory
include/linux/mod_devicetable.h +9 -0
@@ -16,6 +16,13 @@ typedef unsigned long kernel_ulong_t;#define PCI_ANY_ID (~0) + +enum pci_id_flags { +PCI_ID_F_VFIO_DRIVER_OVERRIDE =1 <<0 , +}; + +#define PCI_ID_F_DRIVER_OVERRIDE PCI_ID_F_VFIO_DRIVER_OVERRIDE +/** * struct pci_device_id - PCI device ID structure * @vendor: Vendor ID to match (or PCI_ANY_ID)
@@ -34,12 +41,14 @@ typedef unsigned long kernel_ulong_t;* Best practice is to use driver_data as an index * into a static list of equivalent device types, * instead of using it as a pointer. +* @flags: PCI flags of the driver. Bitmap of pci_id_flags enum. */ struct pci_device_id {__u32 vendor, device;/* Vendor and device ID or PCI_ANY_ID*/ __u32 subvendor, subdevice;/* Subsystem ID's or PCI_ANY_ID */ __u32 class, class_mask;/* (class,subclass,prog-if) triplet */ kernel_ulong_t driver_data;/* Data private to the driver */ +__u32 flags; };
include/linux/pci.h +27 -0
@@ -898,6 +898,33 @@ struct pci_driver { .vendor = (vend), .device = (dev), \ .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID +/** +* PCI_DEVICE_FLAGS - macro used to describe a PCI device with specific flags. +* @vend: the 16 bit PCI Vendor ID +* @dev: the 16 bit PCI Device ID +* @flags: PCI Device flags as a bitmap of pci_id_flags enum +* +* This macro is used to create a struct pci_device_id that matches a +* specific device. The subvendor and subdevice fields will be set to +* PCI_ANY_ID. + */ +#define PCI_DEVICE_FLAGS (vend, dev, fl) \ + .vendor = (vend), .device = (dev), .subvendor = PCI_ANY_ID, \ + .subdevice = PCI_ANY_ID, .flags = (fl) + +/** +* PCI_DRIVER_OVERRIDE_DEVICE_VFIO - macro used to describe a VFIO +* "driver_override" PCI device. +* @vend: the 16 bit PCI Vendor ID +* @dev: the 16 bit PCI Device ID +* +* This macro is used to create a struct pci_device_id that matches a +* specific device. The subvendor and subdevice fields will be set to +* PCI_ANY_ID and the flags will be set to PCI_ID_F_VFIO_DRIVER_OVERRIDE. +*/ +#define PCI_DRIVER_OVERRIDE_DEVICE_VFIO (vend, dev) \ + PCI_DEVICE_FLAGS(vend, dev, PCI_ID_F_VFIO_DRIVER_OVERRIDE) +/** * PCI_DEVICE_SUB - macro used to describe a specific PCI device with subsystem * @vend: the 16 bit PCI Vendor ID
scripts/mod/devicetable-offsets.c +1 -0
@@ -42,6 +42,7 @@ int main(void)DEVID_FIELD (pci_device_id, subdevice);DEVID_FIELD (pci_device_id, class);DEVID_FIELD (pci_device_id, class_mask); +DEVID_FIELD (pci_device_id, flags);DEVID (ccw_device_id);DEVID_FIELD (ccw_device_id, match_flags);
scripts/mod/file2alias.c +6 -2
@@ -426,7 +426,7 @@ static int do_ieee1394_entry(const char *filename,return 1 ; } -/* Looks like: pci:vNdNsvNsdNbcNscNiN. */ +/* Looks like: pci:vNdNsvNsdNbcNscNiN or driver_override_pci:vNdNsvNsdNbcNscNiN. */ static int do_pci_entry (const char *filename,void *symval,char *alias) {
@@ -440,8 +440,12 @@ static int do_pci_entry(const char *filename,DEF_FIELD (symval, pci_device_id, subdevice);DEF_FIELD (symval, pci_device_id, class);DEF_FIELD (symval, pci_device_id, class_mask); +DEF_FIELD (symval, pci_device_id, flags); -strcpy (alias,"pci:" ); +if (flags &PCI_ID_F_VFIO_DRIVER_OVERRIDE ) +strcpy (alias,"vfio_pci:" ); +else +strcpy (alias,"pci:" );ADD (alias,"v" , vendor !=PCI_ANY_ID , vendor);ADD (alias,"d" , device !=PCI_ANY_ID , device);ADD (alias,"sv" , subvendor !=PCI_ANY_ID , subvendor);
Signature · 1 line
2.21.0
PATCH v4 9/11 PCI: add matching checks for driver_override binding
In reply to Max GurtovoyAllowing any driver in the system to be unconditionally bound to any PCI HW is dangerous. Connecting a driver to a physical HW device it was never intended to operate may trigger exploitable kernel bugs, or worse. It also allows userspace to load and run kernel code that otherwise would never be runnable on the system.
driver_override was designed to make it easier to load vfio_pci, so focus it on that single use case. driver_override will only work on drivers that specifically opt into this feature and the driver now has the opportunity to provide a proper match table that indicates what HW it can properly support. vfio-pci continues to support everything.
This also causes the modalias tables to be populated with dedicated match table and userspace now becomes aware that vfio-pci can be loaded against any PCI device using driver_override.
--- Documentation/ABI/testing/sysfs-bus-pci | 6 +++++- drivers/pci/pci-driver.c | 22 ++++++++++++---------- drivers/vfio/pci/vfio_pci.c | 9 ++++++++- 3 files changed, 25 insertions(+), 12 deletions(-)
Documentation/ABI/testing/sysfs-bus-pci +5 -1
@@ -289,7 +289,11 @@ Description: will not bind to any driver. This also allows devices to opt-out of driver binding using a driver_override name such as "none". Only a single driver may be specified in the override, - there is no support for parsing delimiters. + there is no support for parsing delimiters. The binding to the + device is allowed if and only if the matching driver has + enabled the driver_override alias in the ID table (by setting + a suitable bit in the "flags" field of pci_device_id + structure). What: /sys/bus/pci/devices/.../numa_node Date: Oct 2014
drivers/pci/pci-driver.c +12 -10
@@ -115,13 +115,6 @@ const struct pci_device_id *pci_match_id(const struct pci_device_id *ids, }EXPORT_SYMBOL (pci_match_id); -static const struct pci_device_id pci_device_id_any = { - .vendor =PCI_ANY_ID , - .device =PCI_ANY_ID , - .subvendor =PCI_ANY_ID , - .subdevice =PCI_ANY_ID , -}; -/** * pci_match_device - See if a device matches a driver's list of IDs * @drv: the PCI driver to match against
@@ -137,6 +130,7 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv, {struct pci_dynid *dynid;const struct pci_device_id *found_id =NULL ; +bool is_driver_override;/* When driver_override is set, only bind to the matching driver */ if (dev->driver_override &&strcmp (dev->driver_override, drv->name))
@@ -155,9 +149,17 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv,if (!found_id) found_id =pci_match_id (drv->id_table, dev); -/* driver_override will always match, send a dummy id */ -if (!found_id && dev->driver_override) - found_id = &pci_device_id_any; +/* +* if and only if PCI_ID_F_DRIVER_OVERRIDE flag is set, driver_override +* should be provided. +*/ +if (found_id) { + is_driver_override = + (found_id->flags &PCI_ID_F_DRIVER_OVERRIDE ) !=0 ; +if ((is_driver_override && !dev->driver_override) || + (dev->driver_override && !is_driver_override)) +return NULL ; + }return found_id; }
drivers/vfio/pci/vfio_pci.c +8 -1
@@ -166,9 +166,16 @@ static int vfio_pci_sriov_configure(struct pci_dev *pdev, int nr_virtfn)return vfio_pci_core_sriov_configure (pdev, nr_virtfn); } +static const struct pci_device_id vfio_pci_table[] = { + {PCI_DRIVER_OVERRIDE_DEVICE_VFIO (PCI_ANY_ID ,PCI_ANY_ID ) },/* match all by default */ + {0 , } +}; + +MODULE_DEVICE_TABLE (pci, vfio_pci_table); +static struct pci_driver vfio_pci_driver = { .name ="vfio-pci" , - .id_table =NULL ,/* only dynamic ids */ + .id_table = vfio_pci_table, .probe = vfio_pci_probe, .remove = vfio_pci_remove, .sriov_configure = vfio_pci_sriov_configure,
Signature · 1 line
2.21.0
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 5 lines · On Thu, 3 Jun 2021 19:08:07 +0300 Max Gurtovoy <[email protected]> wrote:
Allowing any driver in the system to be unconditionally bound to any PCI HW is dangerous. Connecting a driver to a physical HW device it was never intended to operate may trigger exploitable kernel bugs, or worse. It also allows userspace to load and run kernel code that otherwise would never be runnable on the system.
This is just another way that an admin can do bad things, with the intention that they know what they're doing and if not they get to keep the pieces. There's also still the new_id scheme for binding the wrong drivers to devices, so the hole this claims to be addressing is still fully present.
Quoted 1 line
driver_overridewas designed to make it easier to loadvfio_pci, so
Actually driver_override was designed to resolve the non-deterministic behavior of new_id, which allows inserting dynamic match entries. The problem is those match entries match any device that might come along during the time window when userspace is trying to bind a specific device to a specific driver. driver_override flipped the problem to match a device to a driver rather than vice versa. Other bus types have since adopted driver_override interfaces as well.
Quoted 1 line
focus it on that single use case.
driver_overridewill only work on
It's used for other use cases across numerous bus types now. For instance, how can I user driver_override to bind pci-stub to a device after this? driverctl(8) uses driver_override to perform arbitrary driver overrides, this breaks all but the vfio-pci use case.
Quoted 3 lines
drivers that specifically opt into this feature and the driver now has the opportunity to provide a proper match table that indicates what HW it can properly support. vfio-pci continues to support everything.
In doing so, this also breaks the new_id method for vfio-pci. Sorry, with so many userspace regressions, crippling the driver_override interface with an assumption of such a narrow focus, creating a vfio specific match flag, I don't see where this can go. Thanks,
Alex
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 5 lines · Alex Williamson
Quoted 3 lines
drivers that specifically opt into this feature and the driver now has the opportunity to provide a proper match table that indicates what HW it can properly support. vfio-pci continues to support everything.
In doing so, this also breaks the
new_idmethod for vfio-pci.
Does it? How? The driver_override flag is per match entry not for the entire device so new_id added things will work the same as before as their new match entry's flags will be zero.
Quoted 4 lines
Sorry, with so many userspace regressions, crippling the
driver_overrideinterface with an assumption of such a narrow focus, creating a vfio specific match flag, I don't see where this can go. Thanks,
On the other hand it overcomes all the objections from the last go
round: how userspace figures out which driver to use with driver_override and integrating the universal driver into the scheme.
pci_stub could be delt with by marking it for driver_override like vfio_pci.
But driverctl as a general tool working with any module is not really addressable.
Is the only issue the blocking of the arbitary binding? That is not a critical peice of this, IIRC
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 10 lines · On Tue, 8 Jun 2021 19:45:17 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 5 lines · Alex Williamson
Quoted 3 lines
drivers that specifically opt into this feature and the driver now has the opportunity to provide a proper match table that indicates what HW it can properly support. vfio-pci continues to support everything.
In doing so, this also breaks the
new_idmethod for vfio-pci.Does it? How? The
driver_overrideflag is per match entry not for the entire device sonew_idadded things will work the same as before as their new match entry's flags will be zero.
Hmm, that might have been a testing issue; combining driverctl with manual new_id testing might have left a driver_override in place.
Quoted 11 lines
Quoted 4 lines
Sorry, with so many userspace regressions, crippling the
driver_overrideinterface with an assumption of such a narrow focus, creating a vfio specific match flag, I don't see where this can go. Thanks,On the other hand it overcomes all the objections from the last go
round: how userspace figures out which driver to use withdriver_overrideand integrating the universal driver into the scheme.
pci_stubcould be delt with by marking it fordriver_overridelikevfio_pci.
By marking it a "vfio driver override"? :-\
Quoted 5 lines
But driverctl as a general tool working with any module is not really addressable.
Is the only issue the blocking of the arbitary binding? That is not a critical peice of this, IIRC
We can't break userspace, which means new_id and driver_override need to work as they do now. There are scads of driver binding scripts in the wild, for vfio-pci and other drivers. We can't assume such a narrow scope. Thanks,
Alex
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 18 lines · Alex Williamson
Quoted 8 lines · On Tue, 8 Jun 2021 19:45:17 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 4 lines · Alex Williamson
Quoted 3 lines
drivers that specifically opt into this feature and the driver now has the opportunity to provide a proper match table that indicates what HW it can properly support. vfio-pci continues to support everything.
In doing so, this also breaks the
new_idmethod for vfio-pci.Does it? How? The
driver_overrideflag is per match entry not for the entire device sonew_idadded things will work the same as before as their new match entry's flags will be zero.Hmm, that might have been a testing issue; combining driverctl with manual
new_idtesting might have left adriver_overridein place.Quoted 4 lines
Quoted 4 lines
Sorry, with so many userspace regressions, crippling the
driver_overrideinterface with an assumption of such a narrow focus, creating a vfio specific match flag, I don't see where this can go. Thanks,
Quoted 7 lines
Quoted 6 lines
On the other hand it overcomes all the objections from the last go
round: how userspace figures out which driver to use withdriver_overrideand integrating the universal driver into the scheme.
pci_stubcould be delt with by marking it fordriver_overridelikevfio_pci.By marking it a "vfio driver override"? :-\
Of course not. We'll mark it as "stub driver override".
Quoted 13 lines
Quoted 6 lines
But driverctl as a general tool working with any module is not really addressable.
Is the only issue the blocking of the arbitary binding? That is not a critical peice of this, IIRC
We can't break userspace, which means
new_idanddriver_overrideneed to work as they do now. There are scads of driver binding scripts in the wild, for vfio-pci and other drivers. We can't assume such a narrow scope. Thanks,Alex
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
In reply to Alex WilliamsonQuoted 29 lines · Alex Williamson
Quoted 8 lines · On Tue, 8 Jun 2021 19:45:17 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 4 lines · Alex Williamson
Quoted 3 lines
drivers that specifically opt into this feature and the driver now has the opportunity to provide a proper match table that indicates what HW it can properly support. vfio-pci continues to support everything.
In doing so, this also breaks the
new_idmethod for vfio-pci.Does it? How? The
driver_overrideflag is per match entry not for the entire device sonew_idadded things will work the same as before as their new match entry's flags will be zero.Hmm, that might have been a testing issue; combining driverctl with manual
new_idtesting might have left adriver_overridein place.Quoted 10 lines
Quoted 4 lines
Sorry, with so many userspace regressions, crippling the
driver_overrideinterface with an assumption of such a narrow focus, creating a vfio specific match flag, I don't see where this can go. Thanks,On the other hand it overcomes all the objections from the last go
round: how userspace figures out which driver to use withdriver_overrideand integrating the universal driver into the scheme.
pci_stubcould be delt with by marking it fordriver_overridelikevfio_pci.By marking it a "vfio driver override"? :-\
Quoted 3 lines
But driverctl as a general tool working with any module is not really addressable.
Quoted 6 lines
Quoted 2 lines
Is the only issue the blocking of the arbitary binding? That is not a critical peice of this, IIRC
We can't break userspace, which means
new_idanddriver_overrideneed to work as they do now. There are scads of driver binding scripts in the wild, for vfio-pci and other drivers. We can't assume such a narrow scope. Thanks,
what about the following code ?
@@ -152,12 +152,28 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv, }
spin_unlock(&drv->dynids.lock);
- if (!found_id)
- found_id = pci_match_id(drv->id_table, dev); + if (found_id)
+ return found_id;
- /* driver_override will always match, send a dummy id */ - if (!found_id && dev->driver_override)
+ found_id = pci_match_id(drv->id_table, dev);
+ if (found_id) {
+ /*
+ * if we found id in the static table, we must fulfill the + * matching flags (i.e. if PCI_ID_F_DRIVER_OVERRIDE flag is + * set, driver_override should be provided).
+ */
+ bool is_driver_override =
+ (found_id->flags & PCI_ID_F_DRIVER_OVERRIDE) != 0; + if ((is_driver_override && !dev->driver_override) || + (dev->driver_override && !is_driver_override)) + return NULL;
+ } else if (dev->driver_override) {
+ /*
+ * if we didn't find suitable id in the static table, + * driver_override will still , send a dummy id + */
found_id = &pci_device_id_any;
+ }
return found_id;
}
dynamic ids (new_id) works as before.
Old driver_override works as before.
For "new" driver_override we must fulfill the new rules.
Quoted 3 lines
Alex
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Review static const struct pci_device_id · line 157
Quoted 11 lines · Max Gurtovoy
what about the following code ?
(quoted diff) +2
-2@@ -152,12 +152,28 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv, ?????????????? } ?????????????? spin_unlock(&drv->dynids.lock); -???????????? if (!found_id) -???????????????????????????? found_id = pci_match_id(drv->id_table, dev); +???????????? if (found_id) +???????????????????????????? return found_id;
Something is broken in your mailer because this does not look like code at all.
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 8 lines · Christoph Hellwig
Quoted 7 lines · Max Gurtovoy
what about the following code ?
(quoted diff) +0
-0@@ -152,12 +152,28 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv, ?????????????? } ?????????????? spin_unlock(&drv->dynids.lock);
Quoted 6 lines
Review static const struct pci_device_id · line 157
Quoted 4 lines
(quoted diff) +2
-2-???????????? if (!found_id) -???????????????????????????? found_id = pci_match_id(drv->id_table, dev); +???????????? if (found_id) +???????????????????????????? return found_id;Something is broken in your mailer because this does not look like code at all.
Sorry, see the attached patches.
From 4a6050f0ce50f0cdef4c06d7cabf0260a14a8615 Mon Sep 17 00:00:00 2001
From: Max Gurtovoy <[email protected]>
Date: Thu, 13 May 2021 13:53:19 +0300
Subject: [PATCH 1/2] PCI: add flags field to pci_device_id structure
This field will be used to allow pci modules to set some specific data that can be used for matching, aliases and other hints. Add example for "driver_override" pci drivers that will set a special prefix in the modules.alias table. In the future, this flag will enforce "driver_override" to work on drivers that specifically opt into this feature. The udev utility will not try to load these drivers automatically in order to bind to new discovered devices. This will be because the modalias tables populated by those drivers will be different from "regular" pci modalias tables. Userspace utilities, such as libvirt, will need to adjust and bind devices according to new matching mechanism with taking "driver_override" enforcement into consideration. Add vfio and stub to "driver_override" PCI family.
--- Documentation/PCI/pci.rst | 1 + include/linux/mod_devicetable.h | 11 +++++++++++ include/linux/pci.h | 27 +++++++++++++++++++++++++++ scripts/mod/devicetable-offsets.c | 1 + scripts/mod/file2alias.c | 10 ++++++++-- 5 files changed, 48 insertions(+), 2 deletions(-)
Documentation/PCI/pci.rst +1 -0
@@ -103,6 +103,7 @@ need pass only as many optional fields as necessary: - subvendor and subdevice fields default to PCI_ANY_ID (FFFFFFFF) - class and classmask fields default to 0 - driver_data defaults to 0UL. + - flags field defaults to 0. Note that driver_data must match the value used by any of the pci_device_id entries defined in the driver. This makes the driver_data field mandatory
include/linux/mod_devicetable.h +11 -0
@@ -16,6 +16,15 @@ typedef unsigned long kernel_ulong_t;#define PCI_ANY_ID (~0) + +enum pci_id_flags { +PCI_ID_F_VFIO_DRIVER_OVERRIDE =1 <<0 , +PCI_ID_F_STUB_DRIVER_OVERRIDE =1 <<1 , +}; + +#define PCI_ID_F_DRIVER_OVERRIDE \ + (PCI_ID_F_VFIO_DRIVER_OVERRIDE | PCI_ID_F_STUB_DRIVER_OVERRIDE) +/** * struct pci_device_id - PCI device ID structure * @vendor: Vendor ID to match (or PCI_ANY_ID)
@@ -34,12 +43,14 @@ typedef unsigned long kernel_ulong_t;* Best practice is to use driver_data as an index * into a static list of equivalent device types, * instead of using it as a pointer. +* @flags: PCI flags of the driver. Bitmap of pci_id_flags enum. */ struct pci_device_id {__u32 vendor, device;/* Vendor and device ID or PCI_ANY_ID*/ __u32 subvendor, subdevice;/* Subsystem ID's or PCI_ANY_ID */ __u32 class, class_mask;/* (class,subclass,prog-if) triplet */ kernel_ulong_t driver_data;/* Data private to the driver */ +__u32 flags; };
include/linux/pci.h +27 -0
@@ -898,6 +898,33 @@ struct pci_driver { .vendor = (vend), .device = (dev), \ .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID +/** +* PCI_DEVICE_FLAGS - macro used to describe a PCI device with specific flags. +* @vend: the 16 bit PCI Vendor ID +* @dev: the 16 bit PCI Device ID +* @fl: PCI Device flags as a bitmap of pci_id_flags enum +* +* This macro is used to create a struct pci_device_id that matches a +* specific device. The subvendor and subdevice fields will be set to +* PCI_ANY_ID. + */ +#define PCI_DEVICE_FLAGS (vend, dev, fl) \ + .vendor = (vend), .device = (dev), .subvendor = PCI_ANY_ID, \ + .subdevice = PCI_ANY_ID, .flags = (fl) + +/** +* PCI_DRIVER_OVERRIDE_DEVICE_VFIO - macro used to describe a VFIO +* "driver_override" PCI device. +* @vend: the 16 bit PCI Vendor ID +* @dev: the 16 bit PCI Device ID +* +* This macro is used to create a struct pci_device_id that matches a +* specific device. The subvendor and subdevice fields will be set to +* PCI_ANY_ID and the flags will be set to PCI_ID_F_VFIO_DRIVER_OVERRIDE. +*/ +#define PCI_DRIVER_OVERRIDE_DEVICE_VFIO (vend, dev) \ + PCI_DEVICE_FLAGS(vend, dev, PCI_ID_F_VFIO_DRIVER_OVERRIDE) +/** * PCI_DEVICE_SUB - macro used to describe a specific PCI device with subsystem * @vend: the 16 bit PCI Vendor ID
scripts/mod/devicetable-offsets.c +1 -0
@@ -42,6 +42,7 @@ int main(void)DEVID_FIELD (pci_device_id, subdevice);DEVID_FIELD (pci_device_id, class);DEVID_FIELD (pci_device_id, class_mask); +DEVID_FIELD (pci_device_id, flags);DEVID (ccw_device_id);DEVID_FIELD (ccw_device_id, match_flags);
scripts/mod/file2alias.c +8 -2
@@ -426,7 +426,7 @@ static int do_ieee1394_entry(const char *filename,return 1 ; } -/* Looks like: pci:vNdNsvNsdNbcNscNiN. */ +/* Looks like: pci:vNdNsvNsdNbcNscNiN or <prefix>_pci:vNdNsvNsdNbcNscNiN. */ static int do_pci_entry (const char *filename,void *symval,char *alias) {
@@ -440,8 +440,14 @@ static int do_pci_entry(const char *filename,DEF_FIELD (symval, pci_device_id, subdevice);DEF_FIELD (symval, pci_device_id, class);DEF_FIELD (symval, pci_device_id, class_mask); +DEF_FIELD (symval, pci_device_id, flags); -strcpy (alias,"pci:" ); +if (flags &PCI_ID_F_VFIO_DRIVER_OVERRIDE ) +strcpy (alias,"vfio_pci:" ); +else if (flags &PCI_ID_F_STUB_DRIVER_OVERRIDE ) +strcpy (alias,"stub_pci:" ); +else +strcpy (alias,"pci:" );ADD (alias,"v" , vendor !=PCI_ANY_ID , vendor);ADD (alias,"d" , device !=PCI_ANY_ID , device);ADD (alias,"sv" , subvendor !=PCI_ANY_ID , subvendor);
Signature · 1 line
2.21.0
From 3d49ff43ed2d2c92780090e54a5277a89fe041bf Mon Sep 17 00:00:00 2001
From: Max Gurtovoy <[email protected]>
Date: Thu, 13 May 2021 15:38:27 +0300
Subject: [PATCH 2/2] PCI: add matching checks for driver_override binding
Allowing any driver in the system to be unconditionally bound to any PCI HW is dangerous. Connecting a driver to a physical HW device it was never intended to operate may trigger exploitable kernel bugs, or worse. It also allows userspace to load and run kernel code that otherwise would never be runnable on the system.
Enable the option for drivers to specifically opt into driver_override feature. These drivers will expose a proper matching table that indicates what HW it can properly support. vfio-pci and pci-stub continues to support everything.
The modalias tables to be populated with dedicated match table and userspace now becomes aware that vfio-pci/pci-stub can be loaded against any PCI device using driver_override.
The dynamic new_id option is still might be in use for these driver to preserve backward compatibility.
--- Documentation/ABI/testing/sysfs-bus-pci | 6 +++++- drivers/pci/pci-driver.c | 24 ++++++++++++++++++++---- drivers/pci/pci-stub.c | 9 ++++++++- drivers/vfio/pci/vfio_pci.c | 9 ++++++++- 4 files changed, 41 insertions(+), 7 deletions(-)
Documentation/ABI/testing/sysfs-bus-pci +5 -1
@@ -289,7 +289,11 @@ Description: will not bind to any driver. This also allows devices to opt-out of driver binding using a driver_override name such as "none". Only a single driver may be specified in the override, - there is no support for parsing delimiters. + there is no support for parsing delimiters. For static IDs + matching, this binding mechanism is allowed if and only if the + matching driver has specifically opt into this feature (by + setting a suitable flag in the "flags" field of pci_device_id + structure). What: /sys/bus/pci/devices/.../numa_node Date: Oct 2014
drivers/pci/pci-driver.c +20 -4
@@ -152,12 +152,28 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv, }spin_unlock (&drv->dynids.lock); -if (!found_id) - found_id =pci_match_id (drv->id_table, dev); +if (found_id) +return found_id; -/* driver_override will always match, send a dummy id */ -if (!found_id && dev->driver_override) + found_id =pci_match_id (drv->id_table, dev); +if (found_id) { +/* +* if we found id in the static table, we must fulfill the +* matching flags (i.e. if PCI_ID_F_DRIVER_OVERRIDE flag is +* set, driver_override should be provided). +*/ +bool is_driver_override = + (found_id->flags &PCI_ID_F_DRIVER_OVERRIDE ) !=0 ; +if ((is_driver_override && !dev->driver_override) || + (dev->driver_override && !is_driver_override)) +return NULL ; + }else if (dev->driver_override) { +/* +* if we didn't find suitable id in the static table, +* driver_override will still , send a dummy id +*/ found_id = &pci_device_id_any; + }return found_id; }
drivers/pci/pci-stub.c +8 -1
@@ -26,6 +26,13 @@ MODULE_PARM_DESC(ids, "Initial PCI IDs to add to the stub driver, format is ""\"vendor:device[:subvendor[:subdevice[:class[:class_mask]]]]\"" " and multiple comma separated entries can be specified"); +static const struct pci_device_id stub_pci_table[] = { + {PCI_DEVICE_FLAGS (PCI_ANY_ID ,PCI_ANY_ID ,PCI_ID_F_STUB_DRIVER_OVERRIDE ) },/* match all by default */ + {0 , } +}; + +MODULE_DEVICE_TABLE (pci, stub_pci_table); +static int pci_stub_probe (struct pci_dev *dev,const struct pci_device_id *id) {pci_info (dev,"claimed by stub\n" );
@@ -34,7 +41,7 @@ static int pci_stub_probe(struct pci_dev *dev, const struct pci_device_id *id)static struct pci_driver stub_driver = { .name ="pci-stub" , - .id_table =NULL ,/* only dynamic id's */ + .id_table = stub_pci_table, .probe = pci_stub_probe, };
drivers/vfio/pci/vfio_pci.c +8 -1
@@ -166,9 +166,16 @@ static int vfio_pci_sriov_configure(struct pci_dev *pdev, int nr_virtfn)return vfio_pci_core_sriov_configure (pdev, nr_virtfn); } +static const struct pci_device_id vfio_pci_table[] = { + {PCI_DRIVER_OVERRIDE_DEVICE_VFIO (PCI_ANY_ID ,PCI_ANY_ID ) },/* match all by default */ + {0 , } +}; + +MODULE_DEVICE_TABLE (pci, vfio_pci_table); +static struct pci_driver vfio_pci_driver = { .name ="vfio-pci" , - .id_table =NULL ,/* only dynamic ids */ + .id_table = vfio_pci_table, .probe = vfio_pci_probe, .remove = vfio_pci_remove, .sriov_configure = vfio_pci_sriov_configure,
Signature · 1 line
2.21.0
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
include/linux/mod_devicetable.h typedef unsigned long kernel_ulong_t; · line 54
Quoted 11 lines · Max Gurtovoy
* into a static list of equivalent device types,
* instead of using it as a pointer.
+ * @flags: PCI flags of the driver. Bitmap ofpci_id_flagsenum.
*/
structpci_device_id{include/linux/mod_devicetable.h +1
-0__u32 vendor, device;/* Vendor and device ID or PCI_ANY_ID*/ __u32 subvendor, subdevice;/* Subsystem ID's or PCI_ANY_ID */ __u32 class, class_mask;/* (class,subclass,prog-if) triplet */ kernel_ulong_t driver_data;/* Data private to the driver */ +__u32 flags; };
Isn't struct pci_device_id a userspace ABI due to MODULE_DEVICE_TABLE()?
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 14 lines · Christoph Hellwig
include/linux/mod_devicetable.h typedef unsigned long kernel_ulong_t; · line 54
Quoted 11 lines · Max Gurtovoy
* into a static list of equivalent device types,
* instead of using it as a pointer.
+ * @flags: PCI flags of the driver. Bitmap ofpci_id_flagsenum.
*/
structpci_device_id{include/linux/mod_devicetable.h +1
-0__u32 vendor, device;/* Vendor and device ID or PCI_ANY_ID*/ __u32 subvendor, subdevice;/* Subsystem ID's or PCI_ANY_ID */ __u32 class, class_mask;/* (class,subclass,prog-if) triplet */ kernel_ulong_t driver_data;/* Data private to the driver */ +__u32 flags; };Isn't struct
pci_device_ida userspace ABI due toMODULE_DEVICE_TABLE()?
Not that I can find, it isn't under include/uapi and the way to find this information is by looking for symbols starting with "__mod_"
Debian Code Search says the only place with '"__mod_"' is in file2alias.c at least
Do you know of something? If yes this file should be moved
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 9 lines · Jason Gunthorpe
Quoted 1 line
Isn't struct
pci_device_ida userspace ABI due toMODULE_DEVICE_TABLE()?Not that I can find, it isn't under
include/uapiand the way to find this information is by looking for symbols starting with "__mod_"Debian Code Search says the only place with '"
__mod_"' is infile2alias.cat leastDo you know of something? If yes this file should be moved
Seems lke file2alias.c is indeed the only consumer. So it is a userspace ABI, but ony to a file included in the kernel tree.
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Jason Gunthorpe
Quoted 1 line
Isn't struct
pci_device_ida userspace ABI due toMODULE_DEVICE_TABLE()?Not that I can find, it isn't under
include/uapiand the way to find this information is by looking for symbols starting with "__mod_"
Quoted 7 lines
Quoted 4 lines
Debian Code Search says the only place with '"
__mod_"' is infile2alias.cat leastDo you know of something? If yes this file should be moved
Seems lke
file2alias.cis indeed the only consumer. So it is a userspace ABI, but ony to a file included in the kernel tree.
As I understand it, things are tighter than that, it is only an API between different parts of kbuild - so it is OK to change it. module.alias is the uAPI this data gets marshaled into.
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
In reply to Max GurtovoyQuoted 40 lines · On Sun, 13 Jun 2021 11:19:46 +0300 Max Gurtovoy <[email protected]> wrote:
Quoted 29 lines · Alex Williamson
Quoted 8 lines · On Tue, 8 Jun 2021 19:45:17 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 4 lines · Alex Williamson
Quoted 3 lines
drivers that specifically opt into this feature and the driver now has the opportunity to provide a proper match table that indicates what HW it can properly support. vfio-pci continues to support everything.
In doing so, this also breaks the
new_idmethod for vfio-pci.Does it? How? The
driver_overrideflag is per match entry not for the entire device sonew_idadded things will work the same as before as their new match entry's flags will be zero.Hmm, that might have been a testing issue; combining driverctl with manual
new_idtesting might have left adriver_overridein place.Quoted 10 lines
Quoted 4 lines
Sorry, with so many userspace regressions, crippling the
driver_overrideinterface with an assumption of such a narrow focus, creating a vfio specific match flag, I don't see where this can go. Thanks,On the other hand it overcomes all the objections from the last go
round: how userspace figures out which driver to use withdriver_overrideand integrating the universal driver into the scheme.
pci_stubcould be delt with by marking it fordriver_overridelikevfio_pci.By marking it a "vfio driver override"? :-\
Quoted 3 lines
But driverctl as a general tool working with any module is not really addressable.
Quoted 6 lines
Quoted 2 lines
Is the only issue the blocking of the arbitary binding? That is not a critical peice of this, IIRC
We can't break userspace, which means
new_idanddriver_overrideneed to work as they do now. There are scads of driver binding scripts in the wild, for vfio-pci and other drivers. We can't assume such a narrow scope. Thanks,what about the following code ?
Review static const struct pci_device_id · line 157
Quoted 8 lines
(quoted diff) +2
-2@@ -152,12 +152,28 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv, } spin_unlock(&drv->dynids.lock); - if (!found_id) - found_id = pci_match_id(drv->id_table, dev); + if (found_id) + return found_id;
a) A dynamic ID match always works regardless of driver override...
drivers/pci/pci-driver.c static const struct pci_device_id *pci_match_device(struct pci_driver *drv, · line 159
Quoted 13 lines
drivers/pci/pci-driver.c +10
-2- /* driver_override will always match, send a dummy id */ - if (!found_id && dev->driver_override) + found_id = pci_match_id(drv->id_table, dev); + if (found_id) { + /* + * if we found id in the static table, we must fulfill the + * matching flags (i.e. if PCI_ID_F_DRIVER_OVERRIDE flag is + * set, driver_override should be provided). + */ + bool is_driver_override = + (found_id->flags & PCI_ID_F_DRIVER_OVERRIDE) != 0; + if ((is_driver_override && !dev->driver_override) ||
b) A static ID match fails if the driver provides an override flag and the device does not have an override set, or...
drivers/pci/pci-driver.c static const struct pci_device_id *pci_match_device(struct pci_driver *drv, · line 160
Quoted 1 line
drivers/pci/pci-driver.c +1
-0+ (dev->driver_override && !is_driver_override))
c) The device has an override set and the driver does not support the override flag.
Quoted 13 lines
drivers/pci/pci-driver.c +7
-0+ return NULL; + } else if (dev->driver_override) { + /* + * if we didn't find suitable id in the static table, + * driver_override will still , send a dummy id + */ found_id = &pci_device_id_any; + } return found_id; }
Quoted 3 lines
dynamic ids (
new_id) works as before.Old
driver_overrideworks as before.
This is deceptively complicated, but no, I don't believe it does. By my understanding of c) an "old" driver can no longer use driver_override for binding a known device. It seems that if we have a static ID match, then we cannot have a driver_override set for the device in such a case. This is a userspace regression.
Quoted 1 line
For "new"
driver_overridewe must fulfill the new rules.
For override'able drivers, the static table is almost useless other than using it for modules.alias support and potentially to provide driver_data. As above, I find this all pretty confusing and I'd advise trying to write a concise set of rules outlining the behavior of driver_override vs dynamic IDs vs static IDs vs "override'able" driver flags. I tried, I can't, it's convoluted and full of exceptions. Thanks,
Alex
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 84 lines · Alex Williamson
Quoted 39 lines · On Sun, 13 Jun 2021 11:19:46 +0300 Max Gurtovoy <[email protected]> wrote:
Quoted 29 lines · Alex Williamson
Quoted 8 lines · On Tue, 8 Jun 2021 19:45:17 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 4 lines · Alex Williamson
Quoted 3 lines
drivers that specifically opt into this feature and the driver now has the opportunity to provide a proper match table that indicates what HW it can properly support. vfio-pci continues to support everything.
In doing so, this also breaks the
new_idmethod for vfio-pci.Does it? How? The
driver_overrideflag is per match entry not for the entire device sonew_idadded things will work the same as before as their new match entry's flags will be zero.Hmm, that might have been a testing issue; combining driverctl with manual
new_idtesting might have left adriver_overridein place.Quoted 10 lines
Quoted 4 lines
Sorry, with so many userspace regressions, crippling the
driver_overrideinterface with an assumption of such a narrow focus, creating a vfio specific match flag, I don't see where this can go. Thanks,On the other hand it overcomes all the objections from the last go
round: how userspace figures out which driver to use withdriver_overrideand integrating the universal driver into the scheme.
pci_stubcould be delt with by marking it fordriver_overridelikevfio_pci.By marking it a "vfio driver override"? :-\
Quoted 3 lines
But driverctl as a general tool working with any module is not really addressable.
Quoted 6 lines
Quoted 2 lines
Is the only issue the blocking of the arbitary binding? That is not a critical peice of this, IIRC
We can't break userspace, which means
new_idanddriver_overrideneed to work as they do now. There are scads of driver binding scripts in the wild, for vfio-pci and other drivers. We can't assume such a narrow scope. Thanks,what about the following code ?
Review static const struct pci_device_id · line 157
Quoted 8 lines
(quoted diff) +2
-2@@ -152,12 +152,28 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv, } spin_unlock(&drv->dynids.lock); - if (!found_id) - found_id = pci_match_id(drv->id_table, dev); + if (found_id) + return found_id;a) A dynamic ID match always works regardless of driver override...
drivers/pci/pci-driver.c static const struct pci_device_id *pci_match_device(struct pci_driver *drv, · line 159
Quoted 12 lines
drivers/pci/pci-driver.c +10
-2- /* driver_override will always match, send a dummy id */ - if (!found_id && dev->driver_override) + found_id = pci_match_id(drv->id_table, dev); + if (found_id) { + /* + * if we found id in the static table, we must fulfill the + * matching flags (i.e. if PCI_ID_F_DRIVER_OVERRIDE flag is + * set, driver_override should be provided). + */ + bool is_driver_override = + (found_id->flags & PCI_ID_F_DRIVER_OVERRIDE) != 0; + if ((is_driver_override && !dev->driver_override) ||b) A static ID match fails if the driver provides an override flag and the device does not have an override set, or...
drivers/pci/pci-driver.c static const struct pci_device_id *pci_match_device(struct pci_driver *drv, · line 160
Quoted 1 line
drivers/pci/pci-driver.c +1
-0+ (dev->driver_override && !is_driver_override))c) The device has an override set and the driver does not support the override flag.
Quoted 13 lines
drivers/pci/pci-driver.c +7
-0+ return NULL; + } else if (dev->driver_override) { + /* + * if we didn't find suitable id in the static table, + * driver_override will still , send a dummy id + */ found_id = &pci_device_id_any; + } return found_id; }
Quoted 8 lines
Quoted 3 lines
dynamic ids (
new_id) works as before.Old
driver_overrideworks as before.This is deceptively complicated, but no, I don't believe it does. By my understanding of c) an "old" driver can no longer use
driver_overridefor binding a known device. It seems that if we have a static ID match, then we cannot have adriver_overrideset for the device in such a case. This is a userspace regression.
If I'll remove condition c) everyone will be happy ?
I really would like to end this ongoing discussion and finally have a clear idea of what we want.
By clear I mean C code.
If we'll continue raising ideas we'll never reach our goal. And my goal is the next merge window.
Quoted 12 lines
Quoted 2 lines
For "new"
driver_overridewe must fulfill the new rules.For override'able drivers, the static table is almost useless other than using it for modules.alias support and potentially to provide
driver_data. As above, I find this all pretty confusing and I'd advise trying to write a concise set of rules outlining the behavior ofdriver_overridevs dynamic IDs vs static IDs vs "override'able" driver flags. I tried, I can't, it's convoluted and full of exceptions. Thanks,Alex
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 96 lines · On Tue, 15 Jun 2021 02:12:15 +0300 Max Gurtovoy <[email protected]> wrote:
Quoted 84 lines · Alex Williamson
Quoted 39 lines · On Sun, 13 Jun 2021 11:19:46 +0300 Max Gurtovoy <[email protected]> wrote:
Quoted 29 lines · Alex Williamson
Quoted 8 lines · On Tue, 8 Jun 2021 19:45:17 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 4 lines · Alex Williamson
Quoted 3 lines
drivers that specifically opt into this feature and the driver now has the opportunity to provide a proper match table that indicates what HW it can properly support. vfio-pci continues to support everything.
In doing so, this also breaks the
new_idmethod for vfio-pci.Does it? How? The
driver_overrideflag is per match entry not for the entire device sonew_idadded things will work the same as before as their new match entry's flags will be zero.Hmm, that might have been a testing issue; combining driverctl with manual
new_idtesting might have left adriver_overridein place.Quoted 10 lines
Quoted 4 lines
Sorry, with so many userspace regressions, crippling the
driver_overrideinterface with an assumption of such a narrow focus, creating a vfio specific match flag, I don't see where this can go. Thanks,On the other hand it overcomes all the objections from the last go
round: how userspace figures out which driver to use withdriver_overrideand integrating the universal driver into the scheme.
pci_stubcould be delt with by marking it fordriver_overridelikevfio_pci.By marking it a "vfio driver override"? :-\
Quoted 3 lines
But driverctl as a general tool working with any module is not really addressable.
Quoted 6 lines
Quoted 2 lines
Is the only issue the blocking of the arbitary binding? That is not a critical peice of this, IIRC
We can't break userspace, which means
new_idanddriver_overrideneed to work as they do now. There are scads of driver binding scripts in the wild, for vfio-pci and other drivers. We can't assume such a narrow scope. Thanks,what about the following code ?
Review static const struct pci_device_id · line 157
Quoted 8 lines
(quoted diff) +2
-2@@ -152,12 +152,28 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv, } spin_unlock(&drv->dynids.lock); - if (!found_id) - found_id = pci_match_id(drv->id_table, dev); + if (found_id) + return found_id;a) A dynamic ID match always works regardless of driver override...
Review static const struct pci_device_id · line 167
Quoted 12 lines
(quoted diff) +10
-2- /* driver_override will always match, send a dummy id */ - if (!found_id && dev->driver_override) + found_id = pci_match_id(drv->id_table, dev); + if (found_id) { + /* + * if we found id in the static table, we must fulfill the + * matching flags (i.e. if PCI_ID_F_DRIVER_OVERRIDE flag is + * set, driver_override should be provided). + */ + bool is_driver_override = + (found_id->flags & PCI_ID_F_DRIVER_OVERRIDE) != 0; + if ((is_driver_override && !dev->driver_override) ||b) A static ID match fails if the driver provides an override flag and the device does not have an override set, or...
Review static const struct pci_device_id · line 168
Quoted 1 line
(quoted diff) +1
-0+ (dev->driver_override && !is_driver_override))c) The device has an override set and the driver does not support the override flag.
Quoted 13 lines
(quoted diff) +7
-0+ return NULL; + } else if (dev->driver_override) { + /* + * if we didn't find suitable id in the static table, + * driver_override will still , send a dummy id + */ found_id = &pci_device_id_any; + } return found_id; }Quoted 8 lines
Quoted 3 lines
dynamic ids (
new_id) works as before.Old
driver_overrideworks as before.This is deceptively complicated, but no, I don't believe it does. By my understanding of c) an "old" driver can no longer use
driver_overridefor binding a known device. It seems that if we have a static ID match, then we cannot have adriver_overrideset for the device in such a case. This is a userspace regression.If I'll remove condition c) everyone will be happy ?
Quoted 7 lines
I really would like to end this ongoing discussion and finally have a clear idea of what we want.
By clear I mean C code.
If we'll continue raising ideas we'll never reach our goal. And my goal is the next merge window.
Bjorn would ultimately need to make the call on that, I don't see an obvious regression if c) is dropped. pci-stub and pci-pf-stub should be included in the proposal so we can better understand how creating a "vfio" override in PCI-core plays out for other override types. Also I don't think dynamic IDs should be handled uniquely, new_id_store() should gain support for flags and userspace should be able to add new dynamic ID with override-only matches to the table. Thanks,
Alex
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 4 lines · Alex Williamson
"vfio" override in PCI-core plays out for other override types. Also I don't think dynamic IDs should be handled uniquely,
new_id_store()should gain support for flags and userspace should be able to add new dynamic ID with override-only matches to the table. Thanks,
Why? Once all the enforcement is stripped out the only purpose of the new flag is to signal a different prepration of modules.alias - which won't happen for the new_id path anyhow
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 10 lines · On Tue, 15 Jun 2021 12:04:58 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 4 lines · Alex Williamson
"vfio" override in PCI-core plays out for other override types. Also I don't think dynamic IDs should be handled uniquely,
new_id_store()should gain support for flags and userspace should be able to add new dynamic ID with override-only matches to the table. Thanks,Why? Once all the enforcement is stripped out the only purpose of the new flag is to signal a different prepration of modules.alias - which won't happen for the
new_idpath anyhow
Because new_id allows the admin to insert a new pci_device_id which has been extended to include a flags field and intentionally handling dynamic IDs differently from static IDs seems like generally a bad thing. For example, maybe the admin wants to specify nouveau as only an override match for all 10de: class vga devices. Thanks,
Alex
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 10 lines · Alex Williamson
Quoted 7 lines · On Tue, 15 Jun 2021 12:04:58 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 4 lines · Alex Williamson
"vfio" override in PCI-core plays out for other override types. Also I don't think dynamic IDs should be handled uniquely,
new_id_store()should gain support for flags and userspace should be able to add new dynamic ID with override-only matches to the table. Thanks,
Quoted 8 lines
Quoted 3 lines
Why? Once all the enforcement is stripped out the only purpose of the new flag is to signal a different prepration of modules.alias - which won't happen for the
new_idpath anyhowBecause
new_idallows the admin to insert a newpci_device_idwhich has been extended to include a flags field and intentionally handling dynamic IDs differently from static IDs seems like generally a bad thing.
I'd agree with you if there was a functional difference at runtime, but since that was all removed, I don't think we should touch new_id.
This ends up effectively being only a kbuild related patch that changes how modules.alias is built.
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 20 lines · On Tue, 15 Jun 2021 17:42:16 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 10 lines · Alex Williamson
Quoted 7 lines · On Tue, 15 Jun 2021 12:04:58 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 4 lines · Alex Williamson
"vfio" override in PCI-core plays out for other override types. Also I don't think dynamic IDs should be handled uniquely,
new_id_store()should gain support for flags and userspace should be able to add new dynamic ID with override-only matches to the table. Thanks,Quoted 8 lines
Quoted 3 lines
Why? Once all the enforcement is stripped out the only purpose of the new flag is to signal a different prepration of modules.alias - which won't happen for the
new_idpath anyhowBecause
new_idallows the admin to insert a newpci_device_idwhich has been extended to include a flags field and intentionally handling dynamic IDs differently from static IDs seems like generally a bad thing.
Quoted 5 lines
I'd agree with you if there was a functional difference at runtime, but since that was all removed, I don't think we should touch
new_id.This ends up effectively being only a kbuild related patch that changes how modules.alias is built.
But it wasn't all removed. The proposal had:
a) Short circuit the dynamic ID match
b) Fail a driver-override-only match without a driver_override
c) Fail a non-driver-override-only match with a driver_override
Max is only proposing removing c).
b) alone is a functional, runtime difference. As per my previous example, think about using it as effectively an anti-match. Userspace can create dynamic entries that will be matched before static entries that can demote a driver to not be able to bind to a device automatically. That's functional and useful, I can't think of any other way we have to effectively remove a static match entry from a driver. Thanks,
Alex
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 31 lines · Alex Williamson
Quoted 20 lines · On Tue, 15 Jun 2021 17:42:16 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 10 lines · Alex Williamson
Quoted 7 lines · On Tue, 15 Jun 2021 12:04:58 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 4 lines · Alex Williamson
"vfio" override in PCI-core plays out for other override types. Also I don't think dynamic IDs should be handled uniquely,
new_id_store()should gain support for flags and userspace should be able to add new dynamic ID with override-only matches to the table. Thanks,Quoted 8 lines
Quoted 3 lines
Why? Once all the enforcement is stripped out the only purpose of the new flag is to signal a different prepration of modules.alias - which won't happen for the
new_idpath anyhowBecause
new_idallows the admin to insert a newpci_device_idwhich has been extended to include a flags field and intentionally handling dynamic IDs differently from static IDs seems like generally a bad thing.Quoted 5 lines
I'd agree with you if there was a functional difference at runtime, but since that was all removed, I don't think we should touch
new_id.This ends up effectively being only a kbuild related patch that changes how modules.alias is built.
But it wasn't all removed. The proposal had:
Quoted 7 lines
a) Short circuit the dynamic ID match
b) Fail a driver-override-only match without adriver_override
c) Fail a non-driver-override-only match with adriver_overrideMax is only proposing removing c).
b) alone is a functional, runtime difference.
I would state b) differently:
b) Ignore the driver-override-only match entries in the ID table.
As if we look at new_id, I can't think of any reason for userspace to add an entry to the ID table and then tell the kernel to ignore it. If you want the kernel to ignore it then just don't add it in the first place.
Do you have some other scenario in mind?
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 36 lines · On Tue, 15 Jun 2021 20:00:17 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 35 lines · Alex Williamson
Quoted 20 lines · On Tue, 15 Jun 2021 17:42:16 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 10 lines · Alex Williamson
Quoted 7 lines · On Tue, 15 Jun 2021 12:04:58 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 4 lines · Alex Williamson
"vfio" override in PCI-core plays out for other override types. Also I don't think dynamic IDs should be handled uniquely,
new_id_store()should gain support for flags and userspace should be able to add new dynamic ID with override-only matches to the table. Thanks,Quoted 8 lines
Quoted 3 lines
Why? Once all the enforcement is stripped out the only purpose of the new flag is to signal a different prepration of modules.alias - which won't happen for the
new_idpath anyhowBecause
new_idallows the admin to insert a newpci_device_idwhich has been extended to include a flags field and intentionally handling dynamic IDs differently from static IDs seems like generally a bad thing.Quoted 5 lines
I'd agree with you if there was a functional difference at runtime, but since that was all removed, I don't think we should touch
new_id.This ends up effectively being only a kbuild related patch that changes how modules.alias is built.
But it wasn't all removed. The proposal had:
a) Short circuit the dynamic ID match
b) Fail a driver-override-only match without adriver_override
c) Fail a non-driver-override-only match with adriver_override
Quoted 7 lines
Quoted 3 lines
Max is only proposing removing c).
b) alone is a functional, runtime difference.
I would state b) differently:
b) Ignore the driver-override-only match entries in the ID table.
No, pci_match_device() returns NULL if a match is found that is marked driver-override-only and a driver_override is not specified. That's the same as no match at all. We don't then go on to search past that match in the table, we fail to bind the driver. That's effectively an anti-match when there's no driver_override on the device.
Quoted 6 lines
As if we look at
new_id, I can't think of any reason for userspace to add an entry to the ID table and then tell the kernel to ignore it. If you want the kernel to ignore it then just don't add it in the first place.Do you have some other scenario in mind?
Sure, what if I have two different GPUs in my system, one works fine with the FOSS driver, the other requires a 3rd party driver. I don't want to blacklist the FOSS driver, but I don't want it to claim the other GPU. I can create an anti-match that effectively removes one GPU from the FOSS driver unless it's bound with a driver_override.
I understand that's not your intended use case, but I think this allows that and justifies handling a dynamic ID the same as a static ID. Adding a field to pci_device_id, which is otherwise able to be fully specified via new_id, except for this field, feels like a bug. Thanks,
Alex
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 11 lines · Alex Williamson
Quoted 5 lines
Quoted 1 line
b) alone is a functional, runtime difference.
I would state b) differently:
b) Ignore the driver-override-only match entries in the ID table.
No,
pci_match_device()returns NULL if a match is found that is marked driver-override-only and adriver_overrideis not specified. That's the same as no match at all. We don't then go on to search past that match in the table, we fail to bind the driver. That's effectively an anti-match when there's nodriver_overrideon the device.
anti-match isn't the intention. The deployment will have match tables where all entires are either flags=0 or are driver-override-only.
I would say that mixed match tables make driver-override-only into an anti-match is actually a minor bug in the patch.
The series isn't about adding some new anti-match scheme.
Quoted 4 lines
I understand that's not your intended use case, but I think this allows that and justifies handling a dynamic ID the same as a static ID. Adding a field to
pci_device_id, which is otherwise able to be fully specified vianew_id, except for this field, feels like a bug. Thanks,
Okay, I see what you are saying clearly now.
Your example usage seems legit to me, but I really don't want to entangle it with this series. It is a seperate idea, it can go as a seperate work that uses the new flags and an updated new_id and related parts by someone who wants it.
I hope you'll understand that having NVIDIA Mellanox persue what you describe above is just not going to work..
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 8 lines · On Tue, 15 Jun 2021 20:32:57 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 6 lines · Alex Williamson
Quoted 5 lines
Quoted 1 line
b) alone is a functional, runtime difference.
I would state b) differently:
b) Ignore the driver-override-only match entries in the ID table.
Quoted 8 lines
Quoted 5 lines
No,
pci_match_device()returns NULL if a match is found that is marked driver-override-only and adriver_overrideis not specified. That's the same as no match at all. We don't then go on to search past that match in the table, we fail to bind the driver. That's effectively an anti-match when there's nodriver_overrideon the device.anti-match isn't the intention. The deployment will have match tables where all entires are either flags=0 or are driver-override-only.
I'd expect pci-pf-stub to have one of each, an any-id with override-only flag and the one device ID currently in the table with no flag.
Quoted 12 lines
I would say that mixed match tables make driver-override-only into an anti-match is actually a minor bug in the patch.
The series isn't about adding some new anti-match scheme.
Quoted 4 lines
I understand that's not your intended use case, but I think this allows that and justifies handling a dynamic ID the same as a static ID. Adding a field to
pci_device_id, which is otherwise able to be fully specified vianew_id, except for this field, feels like a bug. Thanks,Okay, I see what you are saying clearly now.
Quoted 7 lines
Your example usage seems legit to me, but I really don't want to entangle it with this series. It is a seperate idea, it can go as a seperate work that uses the new flags and an updated
new_idand related parts by someone who wants it.I hope you'll understand that having NVIDIA Mellanox persue what you describe above is just not going to work..
I understand that use case might hit a nerve, but I don't particularly see why handling static and dynamic IDs consistently wrt to this new flags field is controversial. Thanks,
Alex
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 17 lines · Alex Williamson
Quoted 14 lines · On Tue, 15 Jun 2021 20:32:57 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 11 lines · Alex Williamson
Quoted 5 lines
Quoted 1 line
b) alone is a functional, runtime difference.
I would state b) differently:
b) Ignore the driver-override-only match entries in the ID table.
No,
pci_match_device()returns NULL if a match is found that is marked driver-override-only and adriver_overrideis not specified. That's the same as no match at all. We don't then go on to search past that match in the table, we fail to bind the driver. That's effectively an anti-match when there's nodriver_overrideon the device.
Quoted 6 lines
Quoted 2 lines
anti-match isn't the intention. The deployment will have match tables where all entires are either flags=0 or are driver-override-only.
I'd expect pci-pf-stub to have one of each, an any-id with override-only flag and the one device ID currently in the table with no flag.
Oh Hum. Actually I think this shows the anti-match behavior is actually a bug.. :(
For something like pci_pf_stub_whitelist, if we add a driver_override-only using the PCI any id then it effectively disables new_id completely because the match search will alway find the driver_override match first and stop searching. There is no chance to see things new_id adds.
We have to fix this patch so flags isn't an anti-match to make it work without user regression.
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 20 lines · Jason Gunthorpe
Quoted 14 lines · Alex Williamson
Quoted 11 lines · On Tue, 15 Jun 2021 20:32:57 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 9 lines · Alex Williamson
Quoted 4 lines
Quoted 1 line
b) alone is a functional, runtime difference.
I would state b) differently:
b) Ignore the driver-override-only match entries in the ID table.
No,
pci_match_device()returns NULL if a match is found that is marked driver-override-only and adriver_overrideis not specified. That's the same as no match at all. We don't then go on to search past that match in the table, we fail to bind the driver. That's effectively an anti-match when there's nodriver_overrideon the device.Quoted 5 lines
Quoted 2 lines
anti-match isn't the intention. The deployment will have match tables where all entires are either flags=0 or are driver-override-only.
I'd expect pci-pf-stub to have one of each, an any-id with override-only flag and the one device ID currently in the table with no flag.
Quoted 8 lines
Oh Hum. Actually I think this shows the anti-match behavior is actually a bug.. :(
For something like
pci_pf_stub_whitelist, if we add a driver_override-only using the PCI any id then it effectively disablesnew_idcompletely because the match search will alway find thedriver_overridematch first and stop searching. There is no chance to see thingsnew_idadds.
Actually the dynamic table is the first table the driver search. So new_id works exactly the same AFAIU.
But you're right for static mixed table (I assumed that this will never happen I guess).
If we put the any_id_override id before the non_override AMAZON device entry in the pci-pf-stub we'll fail with the matching to the AMAZON device.
What about the bellow untested addition (also remove condition c):
drivers/pci/pci-driver.c +0 -0
@@ -136,7 +136,7 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
struct pci_dev *dev) {
struct pci_dynid *dynid;
- const struct pci_device_id *found_id = NULL;
+ const struct pci_device_id *found_id = NULL, *ids;
/* When driver_override is set, only bind to the matching driver */
if (dev->driver_override && strcmp(dev->driver_override, drv->name))
@@ -155,8 +155,8 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv, if (found_id)
return found_id;
- found_id = pci_match_id(drv->id_table, dev);
- if (found_id) {
+ ids = drv->id_table;
+ while ((found_id = pci_match_id(ids, dev))) {
/*
* if we found id in the static table, we must fulfill the
* matching flags (i.e. if PCI_ID_F_DRIVER_OVERRIDE flag is
@@ -164,17 +164,19 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv, */
bool is_driver_override =
(found_id->flags & PCI_ID_F_DRIVER_OVERRIDE) != 0;
- if ((is_driver_override && !dev->driver_override) ||
- (dev->driver_override && !is_driver_override))
- return NULL;
- } else if (dev->driver_override) {
- /*
- * if we didn't find suitable id in the static table,
- * driver_override will still , send a dummy id
- */
- found_id = &pci_device_id_any;
+ if (is_driver_override && !dev->driver_override)
+ ids = found_id++; /* continue searching */
+ else
+ break;
}
+ /*
+ * if no static match, driver_override will always match, send a dummy + * id.
+ */
+ if (!found_id && dev->driver_override)
+ found_id = &pci_device_id_any;
+
return found_id;
}
drivers/pci/pci-pf-stub.c +0 -0
@@ -19,6 +19,7 @@
*/
static const struct pci_device_id pci_pf_stub_whitelist[] = {
{ PCI_VDEVICE(AMAZON, 0x0053) },
+ { PCI_DEVICE_FLAGS(PCI_ANY_ID, PCI_ANY_ID, PCI_ID_F_STUB_DRIVER_OVERRIDE) }, /* match all by default (override) */ /* required last entry */
{ 0 }
};
Quoted 5 lines
We have to fix this patch so flags isn't an anti-match to make it work without user regression.
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 25 lines · Max Gurtovoy
Quoted 23 lines · Jason Gunthorpe
Quoted 14 lines · Alex Williamson
Quoted 11 lines · On Tue, 15 Jun 2021 20:32:57 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 9 lines · Alex Williamson
Quoted 4 lines
Quoted 1 line
b) alone is a functional, runtime difference.
I would state b) differently:
b) Ignore the driver-override-only match entries in the ID table.
No,
pci_match_device()returns NULL if a match is found that is marked driver-override-only and adriver_overrideis not specified. That's the same as no match at all. We don't then go on to search past that match in the table, we fail to bind the driver. That's effectively an anti-match when there's nodriver_overrideon the device.Quoted 5 lines
Quoted 2 lines
anti-match isn't the intention. The deployment will have match tables where all entires are either flags=0 or are driver-override-only.
I'd expect pci-pf-stub to have one of each, an any-id with override-only flag and the one device ID currently in the table with no flag.
Oh Hum. Actually I think this shows the anti-match behavior is actually a bug.. :(
Quoted 8 lines
Quoted 5 lines
For something like
pci_pf_stub_whitelist, if we add a driver_override-only using the PCI any id then it effectively disablesnew_idcompletely because the match search will alway find thedriver_overridematch first and stop searching. There is no chance to see thingsnew_idadds.Actually the dynamic table is the first table the driver search. So
new_idworks exactly the same AFAIU.
Oh, even better, so it isn't really an issue
Quoted 2 lines
But you're right for static mixed table (I assumed that this will never happen I guess).
Me too, we could organize the driver-overrides to be last
Review
Quoted 4 lines
(quoted diff) +2
-2- found_id = pci_match_id(drv->id_table, dev); - if (found_id) { + ids = drv->id_table; + while ((found_id = pci_match_id(ids, dev))) {
Yeah, keep searching makes logical sense to me
Quoted 11 lines
drivers/pci/pci-pf-stub.c +2
-0+++ b/drivers/pci/pci-pf-stub.c@@ -19,6 +19,7 @@*/ static const struct pci_device_id pci_pf_stub_whitelist[] = { {PCI_VDEVICE (AMAZON ,0x0053 ) }, + { PCI_DEVICE_FLAGS(PCI_ANY_ID, PCI_ANY_ID,
PCI_ID_F_STUB_DRIVER_OVERRIDE) }, /* match all by default (override) *//* required last entry */ { 0 }
And we don't really want this change any more right? No reason to put pci_stub in the module.alias file?
Thanks,
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 30 lines · Jason Gunthorpe
Quoted 29 lines · Max Gurtovoy
Quoted 28 lines · Jason Gunthorpe
Quoted 14 lines · Alex Williamson
Quoted 11 lines · On Tue, 15 Jun 2021 20:32:57 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 9 lines · Alex Williamson
Quoted 4 lines
Quoted 1 line
b) alone is a functional, runtime difference.
I would state b) differently:
b) Ignore the driver-override-only match entries in the ID table.
No,
pci_match_device()returns NULL if a match is found that is marked driver-override-only and adriver_overrideis not specified. That's the same as no match at all. We don't then go on to search past that match in the table, we fail to bind the driver. That's effectively an anti-match when there's nodriver_overrideon the device.Quoted 5 lines
Quoted 2 lines
anti-match isn't the intention. The deployment will have match tables where all entires are either flags=0 or are driver-override-only.
I'd expect pci-pf-stub to have one of each, an any-id with override-only flag and the one device ID currently in the table with no flag.
Oh Hum. Actually I think this shows the anti-match behavior is actually a bug.. :(
For something like
pci_pf_stub_whitelist, if we add a driver_override-only using the PCI any id then it effectively disablesnew_idcompletely because the match search will alway find thedriver_overridematch first and stop searching. There is no chance to see thingsnew_idadds.
Quoted 7 lines
Quoted 2 lines
Actually the dynamic table is the first table the driver search. So
new_idworks exactly the same AFAIU.Oh, even better, so it isn't really an issue
Quoted 2 lines
But you're right for static mixed table (I assumed that this will never happen I guess).
Me too, we could organize the driver-overrides to be last
Yes we could, but in 2 years from now I'll forget this rule :)
And others may not be aware of it.
Quoted 7 lines
Review
Quoted 5 lines
(quoted diff) +2
-2- found_id = pci_match_id(drv->id_table, dev); - if (found_id) { + ids = drv->id_table; + while ((found_id = pci_match_id(ids, dev))) {Yeah, keep searching makes logical sense to me
Quoted 13 lines
Quoted 11 lines
drivers/pci/pci-pf-stub.c +2
-0+++ b/drivers/pci/pci-pf-stub.c@@ -19,6 +19,7 @@*/ static const struct pci_device_id pci_pf_stub_whitelist[] = { {PCI_VDEVICE (AMAZON ,0x0053 ) }, + { PCI_DEVICE_FLAGS(PCI_ANY_ID, PCI_ANY_ID,
PCI_ID_F_STUB_DRIVER_OVERRIDE) }, /* match all by default (override) *//* required last entry */ { 0 }And we don't really want this change any more right? No reason to put
pci_stubin the module.alias file?
I actually did it in the patches I attached earlier.
It will look like:
stub_pci:v*d*sv*sd*bc*sc*i*
pci:v00001D0Fd00000053sv*sd*bc*sc*i*
I think it's good practice to avoid matching automatically and auto loading any_id_override and vfio_override drivers in general.
Do you see a reason not adding this alias for stub drivers but adding it to vfio_pci drivers ?
Quoted 3 lines
Thanks,
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 2 lines · Max Gurtovoy
Do you see a reason not adding this alias for stub drivers but adding it to
vfio_pcidrivers ?
It creates uABI without a userspace user and that is strongly discouraged. The 'stub_pci:' prefix becomes fixed ABI.
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 6 lines · Jason Gunthorpe
Quoted 2 lines · Max Gurtovoy
Do you see a reason not adding this alias for stub drivers but adding it to
vfio_pcidrivers ?It creates uABI without a userspace user and that is strongly discouraged. The '
stub_pci:' prefix becomes fixed ABI.
so is it better to have "pci:v*d*sv*sd*bc*sc*i*" for stub drivers ? or not adding alias at all if stub flag is set ?
Quoted 2 lines
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 9 lines · Max Gurtovoy
Quoted 6 lines · Jason Gunthorpe
Quoted 2 lines · Max Gurtovoy
Do you see a reason not adding this alias for stub drivers but adding it to
vfio_pcidrivers ?It creates uABI without a userspace user and that is strongly discouraged. The '
stub_pci:' prefix becomes fixed ABI.so is it better to have "pci:v*d*sv*sd*bc*sc*i*" for stub drivers ?
No, we don't want to convey any new information about stub drivers to userspace.
Quoted 1 line
or not adding alias at all if stub flag is set ?
Yes, just don't change it at all, IMHO.
Jason
Re: [PATCH 09/11] PCI: add matching checks for driver_override binding
Quoted 6 lines · Jason Gunthorpe
Quoted 5 lines · Max Gurtovoy
Quoted 4 lines · Jason Gunthorpe
Quoted 2 lines · Max Gurtovoy
Do you see a reason not adding this alias for stub drivers but adding it to
vfio_pcidrivers ?
Quoted 8 lines
Quoted 3 lines
Quoted 2 lines
It creates uABI without a userspace user and that is strongly discouraged. The '
stub_pci:' prefix becomes fixed ABI.so is it better to have "pci:v*d*sv*sd*bc*sc*i*" for stub drivers ?
No, we don't want to convey any new information about stub drivers to userspace.
Quoted 1 line
or not adding alias at all if stub flag is set ?
Yes, just don't change it at all, IMHO.
Ok.
I've attached 2 patches that I would like to agree on before we'll send the V5.
They include the pci-pf-stub additions and keep searching for matching static ids as we discussed.
Max.
Quoted 2 lines
Jason
From 67be1e3a6525c73fa582e8f5aa703b982d6d8114 Mon Sep 17 00:00:00 2001
From: Max Gurtovoy <[email protected]>
Date: Thu, 13 May 2021 13:53:19 +0300
Subject: [PATCH 1/2] PCI: add flags field to pci_device_id structure
This field will be used to allow pci modules to set some specific data that can be used for matching, aliases and other hints. Add example for "driver_override" pci drivers that may want to set a special prefix in the modules.alias table. In the future, this flag will enforce "driver_override" to work on drivers that specifically opt into this feature. In this case, the udev utility will not try to load these drivers automatically in order to bind to new discovered devices. This will be because the modalias tables populated by those drivers will be different from "regular" pci modalias tables. Userspace utilities, such as libvirt for vfio devices, will need to adjust and bind devices according to new matching mechanism with taking "driver_override" enforcement into consideration. Add vfio and stub to "driver_override" PCI family and create new alias for "driver_override" vfio devices.
--- Documentation/PCI/pci.rst | 1 + include/linux/mod_devicetable.h | 11 +++++++++++ include/linux/pci.h | 27 +++++++++++++++++++++++++++ scripts/mod/devicetable-offsets.c | 1 + scripts/mod/file2alias.c | 10 ++++++++-- 5 files changed, 48 insertions(+), 2 deletions(-)
Documentation/PCI/pci.rst +1 -0
@@ -103,6 +103,7 @@ need pass only as many optional fields as necessary: - subvendor and subdevice fields default to PCI_ANY_ID (FFFFFFFF) - class and classmask fields default to 0 - driver_data defaults to 0UL. + - flags field defaults to 0. Note that driver_data must match the value used by any of the pci_device_id entries defined in the driver. This makes the driver_data field mandatory
include/linux/mod_devicetable.h +11 -0
@@ -16,6 +16,15 @@ typedef unsigned long kernel_ulong_t;#define PCI_ANY_ID (~0) + +enum pci_id_flags { +PCI_ID_F_VFIO_DRIVER_OVERRIDE =1 <<0 , +PCI_ID_F_STUB_DRIVER_OVERRIDE =1 <<1 , +}; + +#define PCI_ID_F_DRIVER_OVERRIDE \ + (PCI_ID_F_VFIO_DRIVER_OVERRIDE | PCI_ID_F_STUB_DRIVER_OVERRIDE) +/** * struct pci_device_id - PCI device ID structure * @vendor: Vendor ID to match (or PCI_ANY_ID)
@@ -34,12 +43,14 @@ typedef unsigned long kernel_ulong_t;* Best practice is to use driver_data as an index * into a static list of equivalent device types, * instead of using it as a pointer. +* @flags: PCI flags of the driver. Bitmap of pci_id_flags enum. */ struct pci_device_id {__u32 vendor, device;/* Vendor and device ID or PCI_ANY_ID*/ __u32 subvendor, subdevice;/* Subsystem ID's or PCI_ANY_ID */ __u32 class, class_mask;/* (class,subclass,prog-if) triplet */ kernel_ulong_t driver_data;/* Data private to the driver */ +__u32 flags; };
include/linux/pci.h +27 -0
@@ -898,6 +898,33 @@ struct pci_driver { .vendor = (vend), .device = (dev), \ .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID +/** +* PCI_DEVICE_FLAGS - macro used to describe a PCI device with specific flags. +* @vend: the 16 bit PCI Vendor ID +* @dev: the 16 bit PCI Device ID +* @fl: PCI Device flags as a bitmap of pci_id_flags enum +* +* This macro is used to create a struct pci_device_id that matches a +* specific device. The subvendor and subdevice fields will be set to +* PCI_ANY_ID. + */ +#define PCI_DEVICE_FLAGS (vend, dev, fl) \ + .vendor = (vend), .device = (dev), .subvendor = PCI_ANY_ID, \ + .subdevice = PCI_ANY_ID, .flags = (fl) + +/** +* PCI_DRIVER_OVERRIDE_DEVICE_VFIO - macro used to describe a VFIO +* "driver_override" PCI device. +* @vend: the 16 bit PCI Vendor ID +* @dev: the 16 bit PCI Device ID +* +* This macro is used to create a struct pci_device_id that matches a +* specific device. The subvendor and subdevice fields will be set to +* PCI_ANY_ID and the flags will be set to PCI_ID_F_VFIO_DRIVER_OVERRIDE. +*/ +#define PCI_DRIVER_OVERRIDE_DEVICE_VFIO (vend, dev) \ + PCI_DEVICE_FLAGS(vend, dev, PCI_ID_F_VFIO_DRIVER_OVERRIDE) +/** * PCI_DEVICE_SUB - macro used to describe a specific PCI device with subsystem * @vend: the 16 bit PCI Vendor ID
scripts/mod/devicetable-offsets.c +1 -0
@@ -42,6 +42,7 @@ int main(void)DEVID_FIELD (pci_device_id, subdevice);DEVID_FIELD (pci_device_id, class);DEVID_FIELD (pci_device_id, class_mask); +DEVID_FIELD (pci_device_id, flags);DEVID (ccw_device_id);DEVID_FIELD (ccw_device_id, match_flags);
scripts/mod/file2alias.c +8 -2
@@ -426,7 +426,7 @@ static int do_ieee1394_entry(const char *filename,return 1 ; } -/* Looks like: pci:vNdNsvNsdNbcNscNiN. */ +/* Looks like: pci:vNdNsvNsdNbcNscNiN or <prefix>_pci:vNdNsvNsdNbcNscNiN. */ static int do_pci_entry (const char *filename,void *symval,char *alias) {
@@ -440,8 +440,14 @@ static int do_pci_entry(const char *filename,DEF_FIELD (symval, pci_device_id, subdevice);DEF_FIELD (symval, pci_device_id, class);DEF_FIELD (symval, pci_device_id, class_mask); +DEF_FIELD (symval, pci_device_id, flags); -strcpy (alias,"pci:" ); +if (!flags) +strcpy (alias,"pci:" ); +else if (flags &PCI_ID_F_VFIO_DRIVER_OVERRIDE ) +strcpy (alias,"vfio_pci:" ); +else +return 0 ;ADD (alias,"v" , vendor !=PCI_ANY_ID , vendor);ADD (alias,"d" , device !=PCI_ANY_ID , device);ADD (alias,"sv" , subvendor !=PCI_ANY_ID , subvendor);
Signature · 1 line
2.18.1
From dbad3ab97af39455c4e4f6967bdd47ef1ea67198 Mon Sep 17 00:00:00 2001
From: Max Gurtovoy <[email protected]>
Date: Thu, 13 May 2021 15:38:27 +0300
Subject: [PATCH 2/2] PCI: add matching checks for driver_override binding
Allowing any driver in the system to be unconditionally bound to any PCI HW is dangerous. Connecting a driver to a physical HW device it was never intended to operate may trigger exploitable kernel bugs, or worse. It also allows userspace to load and run kernel code that otherwise would never be runnable on the system.
Enable the option for drivers to specifically opt into driver_override feature. These drivers will expose a proper matching table that indicates what HW it can properly support. vfio-pci and pci stub drivers continues to support everything.
The modalias tables to be populated with dedicated match table and userspace now becomes aware that vfio-pci can be loaded against any PCI device using driver_override (modalias for pci stub drivers can be added in the future, if needed).
The dynamic new_id option is still might be in use for these drivers to preserve backward compatibility. For now, also preserve the old driver_override mechanism for non "driver_override" modules.
--- Documentation/ABI/testing/sysfs-bus-pci | 5 ++++- drivers/pci/pci-driver.c | 26 +++++++++++++++++++++---- drivers/pci/pci-pf-stub.c | 1 + drivers/pci/pci-stub.c | 9 ++++++++- drivers/vfio/pci/vfio_pci.c | 9 ++++++++- 5 files changed, 43 insertions(+), 7 deletions(-)
Documentation/ABI/testing/sysfs-bus-pci +4 -1
@@ -289,7 +289,10 @@ Description: will not bind to any driver. This also allows devices to opt-out of driver binding using a driver_override name such as "none". Only a single driver may be specified in the override, - there is no support for parsing delimiters. + there is no support for parsing delimiters. For static IDs + matching, this binding mechanism is allowed if the matching + driver has specifically opt into this feature (by setting a + suitable flag in the "flags" field of pci_device_id structure). What: /sys/bus/pci/devices/.../numa_node Date: Oct 2014
drivers/pci/pci-driver.c +22 -4
@@ -136,7 +136,7 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv, struct pci_dev *dev) {struct pci_dynid *dynid; -const struct pci_device_id *found_id =NULL ; +const struct pci_device_id *found_id =NULL , *ids;/* When driver_override is set, only bind to the matching driver */ if (dev->driver_override &&strcmp (dev->driver_override, drv->name))
@@ -152,10 +152,28 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv, }spin_unlock (&drv->dynids.lock); -if (!found_id) - found_id =pci_match_id (drv->id_table, dev); +if (found_id) +return found_id; -/* driver_override will always match, send a dummy id */ + ids = drv->id_table; +while ((found_id =pci_match_id (ids, dev))) { +/* +* if we found id in the static table, we must fulfill the +* matching flags (i.e. if PCI_ID_F_DRIVER_OVERRIDE flag is +* set, driver_override should be provided). +*/ +bool is_driver_override = + (found_id->flags &PCI_ID_F_DRIVER_OVERRIDE ) !=0 ; +if (is_driver_override && !dev->driver_override) + ids = found_id +1 ;/* continue searching */ +else +break ;/* found a match ! */ + } + +/* +* if no static match, driver_override will always match, send a dummy +* id. +*/ if (!found_id && dev->driver_override) found_id = &pci_device_id_any;
drivers/pci/pci-pf-stub.c +1 -0
@@ -19,6 +19,7 @@*/ static const struct pci_device_id pci_pf_stub_whitelist[] = { {PCI_VDEVICE (AMAZON ,0x0053 ) }, + {PCI_DEVICE_FLAGS (PCI_ANY_ID ,PCI_ANY_ID ,PCI_ID_F_STUB_DRIVER_OVERRIDE ) },/* match all by default (override) */ /* required last entry */ {0 } };
drivers/pci/pci-stub.c +8 -1
@@ -26,6 +26,13 @@ MODULE_PARM_DESC(ids, "Initial PCI IDs to add to the stub driver, format is ""\"vendor:device[:subvendor[:subdevice[:class[:class_mask]]]]\"" " and multiple comma separated entries can be specified"); +static const struct pci_device_id stub_pci_table[] = { + {PCI_DEVICE_FLAGS (PCI_ANY_ID ,PCI_ANY_ID ,PCI_ID_F_STUB_DRIVER_OVERRIDE ) },/* match all by default */ + {0 , } +}; + +MODULE_DEVICE_TABLE (pci, stub_pci_table); +static int pci_stub_probe (struct pci_dev *dev,const struct pci_device_id *id) {pci_info (dev,"claimed by stub\n" );
@@ -34,7 +41,7 @@ static int pci_stub_probe(struct pci_dev *dev, const struct pci_device_id *id)static struct pci_driver stub_driver = { .name ="pci-stub" , - .id_table =NULL ,/* only dynamic id's */ + .id_table = stub_pci_table, .probe = pci_stub_probe, };
drivers/vfio/pci/vfio_pci.c +8 -1
@@ -166,9 +166,16 @@ static int vfio_pci_sriov_configure(struct pci_dev *pdev, int nr_virtfn)return vfio_pci_core_sriov_configure (pdev, nr_virtfn); } +static const struct pci_device_id vfio_pci_table[] = { + {PCI_DRIVER_OVERRIDE_DEVICE_VFIO (PCI_ANY_ID ,PCI_ANY_ID ) },/* match all by default */ + {0 , } +}; + +MODULE_DEVICE_TABLE (pci, vfio_pci_table); +static struct pci_driver vfio_pci_driver = { .name ="vfio-pci" , - .id_table =NULL ,/* only dynamic ids */ + .id_table = vfio_pci_table, .probe = vfio_pci_probe, .remove = vfio_pci_remove, .sriov_configure = vfio_pci_sriov_configure,
Signature · 1 line
2.18.1
PATCH v4 10/11 vfio-pci: introduce vfio_pci_core subsystem driver
In reply to Max GurtovoyNow that vfio_pci has been split into two source modules, one focusing on the "struct pci_driver" (vfio_pci.c) and a toolbox library of code (vfio_pci_core.c), complete the split and move them into two different modules.
As before vfio_pci.ko continues to present the same interface under sysfs and this change will have no functional impact.
Below is an example for adding new driver that will use vfio pci subsystem:
+---------------------------------------------------+ | | | VFIO | | | +---------------------------------------------------+
+---------------------------------------------------+ | | | VFIO_PCI_CORE | | | +---------------------------------------------------+
+----------+ +---------------+ +--------------------+ | | | | | | | VFIO_PCI | | MLX5_VFIO_PCI | | HISILICON_VFIO_PCI | | | | | | | +----------+ +---------------+ +--------------------+
Splitting into another module and adding exports allows creating new HW specific vfio_pci drivers that can implement device specific functionality, such as VFIO migration interfaces or specialized device requirements.
---
drivers/vfio/pci/Kconfig | 18 ++++++---
drivers/vfio/pci/Makefile | 11 +++--
drivers/vfio/pci/vfio_pci.c | 14 ++-----
drivers/vfio/pci/vfio_pci_config.c | 2 +-
drivers/vfio/pci/vfio_pci_core.c | 40 ++++++++++++++++---
drivers/vfio/pci/vfio_pci_igd.c | 2 +-
drivers/vfio/pci/vfio_pci_intrs.c | 2 +-
drivers/vfio/pci/vfio_pci_rdwr.c | 2 +-
drivers/vfio/pci/vfio_pci_zdev.c | 2 +-
.../pci => include/linux}/vfio_pci_core.h | 10 ++---
10 files changed, 66 insertions(+), 37 deletions(-)
rename {drivers/vfio/pci => include/linux}/vfio_pci_core.h (96%)drivers/vfio/pci/Kconfig +13 -5
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only -config VFIO_PCI - tristate "VFIO support for PCI devices" +config VFIO_PCI_CORE + tristate "VFIO core support for PCI devices" depends on VFIO && PCI && EVENTFD depends on MMU select VFIO_VIRQFD
@@ -11,9 +11,17 @@ config VFIO_PCI If you don't know what to do here, say N. +config VFIO_PCI + tristate "VFIO support for PCI devices" + depends on VFIO_PCI_CORE + help + This provides a generic PCI support using the VFIO framework. + + If you don't know what to do here, say N. + config VFIO_PCI_VGA bool "VFIO PCI support for VGA devices" - depends on VFIO_PCI && X86 && VGA_ARB + depends on VFIO_PCI_CORE && X86 && VGA_ARB help Support for VGA extension to VFIO PCI. This exposes an additional region on VGA devices for accessing legacy VGA addresses used by
@@ -22,11 +30,11 @@ config VFIO_PCI_VGA If you don't know what to do here, say N. config VFIO_PCI_MMAP - depends on VFIO_PCI + depends on VFIO_PCI_CORE def_bool y if !S390 config VFIO_PCI_INTX - depends on VFIO_PCI + depends on VFIO_PCI_CORE def_bool y if !S390 config VFIO_PCI_IGD
drivers/vfio/pci/Makefile +7 -4
@@ -1,7 +1,10 @@ # SPDX-License-Identifier: GPL-2.0-only -vfio-pci-y := vfio_pci.o vfio_pci_core.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o -vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o -vfio-pci-$(CONFIG_S390) += vfio_pci_zdev.o - +obj-$(CONFIG_VFIO_PCI_CORE) += vfio-pci-core.o obj-$(CONFIG_VFIO_PCI) += vfio-pci.o + +vfio-pci-core-y := vfio_pci_core.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o +vfio-pci-core-$(CONFIG_S390) += vfio_pci_zdev.o + +vfio-pci-y := vfio_pci.o +vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
drivers/vfio/pci/vfio_pci.c +3 -11
@@ -25,7 +25,7 @@#include <linux/types.h> #include <linux/uaccess.h> -#include "vfio_pci_core.h" +#include <linux/vfio_pci_core.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"
@@ -141,6 +141,7 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (ret) goto out_free; +dev_set_drvdata (&pdev->dev, vdev);return 0 ; out_free:
@@ -185,7 +186,6 @@ static struct pci_driver vfio_pci_driver = { static void __exit vfio_pci_cleanup(void) {pci_unregister_driver (&vfio_pci_driver); -vfio_pci_core_cleanup (); }static void __init vfio_pci_fill_ids (void )
@@ -233,14 +233,10 @@ static int __init vfio_pci_init(void) {int ret; - ret =vfio_pci_core_init (); -if (ret) -return ret; -/* Register and scan for devices */ ret =pci_register_driver (&vfio_pci_driver);if (ret) - goto out; +return ret;vfio_pci_fill_ids ();
@@ -248,10 +244,6 @@ static int __init vfio_pci_init(void)pr_warn ("device denylist disabled.\n" );return 0 ; - -out: -vfio_pci_core_cleanup (); -return ret; }module_init (vfio_pci_init);
drivers/vfio/pci/vfio_pci_config.c +1 -1
@@ -26,7 +26,7 @@#include <linux/vfio.h> #include <linux/slab.h> -#include "vfio_pci_core.h" +#include <linux/vfio_pci_core.h> /* Fake capability ID for standard config space */ #define PCI_CAP_ID_BASIC 0
drivers/vfio/pci/vfio_pci_core.c +34 -6
@@ -8,6 +8,8 @@* Author: Tom Lyon, [email protected] */ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/device.h> #include <linux/eventfd.h> #include <linux/file.h>
@@ -25,7 +27,11 @@#include <linux/nospec.h> #include <linux/sched/mm.h> -#include "vfio_pci_core.h" +#include <linux/vfio_pci_core.h> + +#define DRIVER_VERSION "0.2" +#define DRIVER_AUTHOR "Alex Williamson <[email protected]>" +#define DRIVER_DESC "core driver for VFIO based PCI devices"static bool nointxmask;module_param_named (nointxmask, nointxmask, bool,S_IRUGO |S_IWUSR );
@@ -316,6 +322,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)return 0 ; } +EXPORT_SYMBOL_GPL (vfio_pci_core_enable);void vfio_pci_core_disable (struct vfio_pci_core_device *vdev) {
@@ -415,6 +422,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)if (!disable_idle_d3)vfio_pci_set_power_state (vdev, PCI_D3hot); } +EXPORT_SYMBOL_GPL (vfio_pci_core_disable);static struct vfio_pci_core_device *get_pf_vdev (struct vfio_pci_core_device *vdev) {
@@ -473,6 +481,7 @@ void vfio_pci_core_release(struct vfio_device *core_vdev) }mutex_unlock (&vdev->igate); } +EXPORT_SYMBOL_GPL (vfio_pci_core_release);void vfio_pci_core_finish_enable (struct vfio_pci_core_device *vdev) {
@@ -480,6 +489,7 @@ void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev)vfio_spapr_pci_eeh_open (vdev->pdev);vfio_pci_vf_token_user_add (vdev,1 ); } +EXPORT_SYMBOL_GPL (vfio_pci_core_finish_enable);int vfio_pci_core_open (struct vfio_device *core_vdev) {
@@ -497,6 +507,7 @@ int vfio_pci_core_open(struct vfio_device *core_vdev)return 0 ; } +EXPORT_SYMBOL_GPL (vfio_pci_core_open);static int vfio_pci_get_irq_count (struct vfio_pci_core_device *vdev,int irq_type) {
@@ -681,6 +692,7 @@ int vfio_pci_register_dev_region(struct vfio_pci_core_device *vdev,return 0 ; } +EXPORT_SYMBOL_GPL (vfio_pci_register_dev_region);struct vfio_devices {struct vfio_pci_core_device **devices;
@@ -1287,6 +1299,7 @@ long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd,return -ENOTTY ; } +EXPORT_SYMBOL_GPL (vfio_pci_core_ioctl);static ssize_t vfio_pci_rw (struct vfio_pci_core_device *vdev,char __user *buf,size_t count,loff_t *ppos,bool iswrite)
@@ -1330,6 +1343,7 @@ ssize_t vfio_pci_core_read(struct vfio_device *core_vdev, char __user *buf,return vfio_pci_rw (vdev, buf, count, ppos, false); } +EXPORT_SYMBOL_GPL (vfio_pci_core_read);ssize_t vfio_pci_core_write (struct vfio_device *core_vdev,const char __user *buf,size_t count,loff_t *ppos)
@@ -1342,6 +1356,7 @@ ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *bureturn vfio_pci_rw (vdev, (char __user *)buf, count, ppos, true); } +EXPORT_SYMBOL_GPL (vfio_pci_core_write);/* Return 1 on zap and vma_lock acquired, 0 on contention (only with @try) */ static int vfio_pci_zap_and_vma_lock (struct vfio_pci_core_device *vdev,bool try)
@@ -1607,6 +1622,7 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vmareturn 0 ; } +EXPORT_SYMBOL_GPL (vfio_pci_core_mmap);void vfio_pci_core_request (struct vfio_device *core_vdev,unsigned int count) {
@@ -1629,6 +1645,7 @@ void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count)mutex_unlock (&vdev->igate); } +EXPORT_SYMBOL_GPL (vfio_pci_core_request);static int vfio_pci_validate_vf_token (struct vfio_pci_core_device *vdev,bool vf_token,uuid_t *uuid)
@@ -1773,6 +1790,7 @@ int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf)return 1 ;/* Match */ } +EXPORT_SYMBOL_GPL (vfio_pci_core_match);static int vfio_pci_reflck_find (struct pci_dev *pdev,void *data) {
@@ -1814,6 +1832,7 @@ int vfio_pci_core_reflck_attach(struct vfio_device *core_vdev)return PTR_ERR_OR_ZERO (core_vdev->reflck); } +EXPORT_SYMBOL_GPL (vfio_pci_core_reflck_attach);static int vfio_pci_bus_notifier (struct notifier_block *nb,unsigned long action,void *data)
@@ -1972,7 +1991,6 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev, ret =vfio_register_group_dev (&vdev->vdev);if (ret) goto out_power; -dev_set_drvdata (&pdev->dev, vdev);return 0 ; out_power:
@@ -1987,6 +2005,7 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev,vfio_iommu_group_put (group, &pdev->dev);return ret; } +EXPORT_SYMBOL_GPL (vfio_pci_core_register_device);void vfio_pci_core_unregister_device (struct vfio_pci_core_device *vdev) {
@@ -2009,6 +2028,7 @@ void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev)kfree (vdev->region);kfree (vdev->pm_save); } +EXPORT_SYMBOL_GPL (vfio_pci_core_unregister_device);static pci_ers_result_t vfio_pci_aer_err_detected (struct pci_dev *pdev,pci_channel_state_t state)
@@ -2054,10 +2074,12 @@ int vfio_pci_core_sriov_configure(struct pci_dev *pdev, int nr_virtfn)return ret <0 ? ret : nr_virtfn; } +EXPORT_SYMBOL_GPL (vfio_pci_core_sriov_configure);const struct pci_error_handlers vfio_pci_core_err_handlers = { .error_detected = vfio_pci_aer_err_detected, }; +EXPORT_SYMBOL_GPL (vfio_pci_core_err_handlers);static int vfio_pci_get_unused_devs (struct pci_dev *pdev,void *data) {
@@ -2197,15 +2219,21 @@ static void vfio_pci_try_bus_reset(struct vfio_pci_core_device *vdev)kfree (devs.devices); } -/* This will become the __exit function of vfio_pci_core.ko */ -void vfio_pci_core_cleanup (void ) +static void vfio_pci_core_cleanup (void ) {vfio_pci_uninit_perm_bits (); } -/* This will become the __init function of vfio_pci_core.ko */ -int __init vfio_pci_core_init (void ) +static int __init vfio_pci_core_init (void ) {/* Allocate shared config space permission data used by all devices */ return vfio_pci_init_perm_bits (); } + +module_init (vfio_pci_core_init); +module_exit (vfio_pci_core_cleanup); + +MODULE_VERSION (DRIVER_VERSION ); +MODULE_LICENSE ("GPL v2" ); +MODULE_AUTHOR (DRIVER_AUTHOR ); +MODULE_DESCRIPTION (DRIVER_DESC );
drivers/vfio/pci/vfio_pci_igd.c +1 -1
@@ -15,7 +15,7 @@#include <linux/uaccess.h> #include <linux/vfio.h> -#include "vfio_pci_core.h" +#include <linux/vfio_pci_core.h> #define OPREGION_SIGNATURE "IntelGraphicsMem"#define OPREGION_SIZE (8 * 1024)
drivers/vfio/pci/vfio_pci_intrs.c +1 -1
@@ -20,7 +20,7 @@#include <linux/wait.h> #include <linux/slab.h> -#include "vfio_pci_core.h" +#include <linux/vfio_pci_core.h> /* * INTx
drivers/vfio/pci/vfio_pci_rdwr.c +1 -1
@@ -17,7 +17,7 @@#include <linux/vfio.h> #include <linux/vgaarb.h> -#include "vfio_pci_core.h" +#include <linux/vfio_pci_core.h> #ifdef __LITTLE_ENDIAN#define vfio_ioread64 ioread64
drivers/vfio/pci/vfio_pci_zdev.c +1 -1
@@ -19,7 +19,7 @@#include <asm/pci_clp.h> #include <asm/pci_io.h> -#include "vfio_pci_core.h" +#include <linux/vfio_pci_core.h> /* * Add the Base PCI Function information to the device info region.
include/linux/vfio_pci_core.h renamed from drivers/vfio/pci/vfio_pci_core.h +4 -6
@@ -171,10 +171,10 @@ extern void vfio_pci_uninit_perm_bits(void);extern int vfio_config_init (struct vfio_pci_core_device *vdev);extern void vfio_config_free (struct vfio_pci_core_device *vdev); -extern int vfio_pci_register_dev_region (struct vfio_pci_core_device *vdev, -unsigned int type,unsigned int subtype, -const struct vfio_pci_regops *ops, -size_t size,u32 flags,void *data); +int vfio_pci_register_dev_region (struct vfio_pci_core_device *vdev, +unsigned int type,unsigned int subtype, +const struct vfio_pci_regops *ops, +size_t size,u32 flags,void *data);extern int vfio_pci_set_power_state (struct vfio_pci_core_device *vdev,pci_power_t state);
@@ -207,8 +207,6 @@ static inline int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,#endif /* Will be exported for vfio pci drivers usage */ -void vfio_pci_core_cleanup (void ); -int vfio_pci_core_init (void );void vfio_pci_core_release (struct vfio_device *core_vdev);int vfio_pci_core_open (struct vfio_device *core_vdev); int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev,
Signature · 1 line
2.21.0
Re: [PATCH 10/11] vfio-pci: introduce vfio_pci_core subsystem driver
Quoted 15 lines · On Thu, 3 Jun 2021 19:08:08 +0300 Max Gurtovoy <[email protected]> wrote:
drivers/vfio/pci/Kconfig +2
-2@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only -config VFIO_PCI - tristate "VFIO support for PCI devices" +config VFIO_PCI_CORE + tristate "VFIO core support for PCI devices" depends on VFIO && PCI && EVENTFD depends on MMU select VFIO_VIRQFD@@ -11,9 +11,17 @@ config VFIO_PCI
drivers/vfio/pci/Kconfig config VFIO_PCI · line 21
Quoted 10 lines
drivers/vfio/pci/Kconfig +8
-0If you don't know what to do here, say N. +config VFIO_PCI + tristate "VFIO support for PCI devices" + depends on VFIO_PCI_CORE + help + This provides a generic PCI support using the VFIO framework. + + If you don't know what to do here, say N. +
I think it's going to generate a lot of user and distro frustration to hide VFIO_PCI behind a new VFIO_PCI_CORE option. The core should be a dependency *selected* by the drivers, not a prerequisite for the driver. Thanks,
Alex
Re: [PATCH 10/11] vfio-pci: introduce vfio_pci_core subsystem driver
Quoted 19 lines · Alex Williamson
Quoted 17 lines · On Thu, 3 Jun 2021 19:08:08 +0300 Max Gurtovoy <[email protected]> wrote:
drivers/vfio/pci/Kconfig +2
-2@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only -config VFIO_PCI - tristate "VFIO support for PCI devices" +config VFIO_PCI_CORE + tristate "VFIO core support for PCI devices" depends on VFIO && PCI && EVENTFD depends on MMU select VFIO_VIRQFD@@ -11,9 +11,17 @@ config VFIO_PCI If you don't know what to do here, say N.
Quoted 12 lines
drivers/vfio/pci/Kconfig config VFIO_PCI · line 21
Quoted 8 lines
drivers/vfio/pci/Kconfig +8
-0+config VFIO_PCI + tristate "VFIO support for PCI devices" + depends on VFIO_PCI_CORE + help + This provides a generic PCI support using the VFIO framework. + + If you don't know what to do here, say N. +I think it's going to generate a lot of user and distro frustration to hide
VFIO_PCIbehind a newVFIO_PCI_COREoption. The core should be a dependency *selected* by the drivers, not a prerequisite for the driver. Thanks,
I'll fix that. Thanks.
Quoted 3 lines
Alex
PATCH v4 11/11 mlx5-vfio-pci: add new vfio_pci driver for mlx5 devices
In reply to Max GurtovoyThis is the initial step for adding vendor specific vfio_pci driver for mlx5 devices. mlx5_vfio_pci use vfio_pci_core to register to the VFIO subsystem and also to implement the basic functionality of a pci device.
It will also extend this basic functionality and add mlx5 specific logic for various features (such as live migration, for example).
Note: Although we've created the mlx5-vfio-pci.ko, the binding to vfio-pci.ko will still work as before. It's fully backward compatible. Of course, the extended vendor functionality will not exist in case one will bind the device to the generic vfio_pci.ko.
--- drivers/vfio/pci/Kconfig | 9 +++ drivers/vfio/pci/Makefile | 3 + drivers/vfio/pci/mlx5_vfio_pci.c | 130 +++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 drivers/vfio/pci/mlx5_vfio_pci.c
drivers/vfio/pci/Kconfig +9 -0
@@ -48,3 +48,12 @@ config VFIO_PCI_IGD and LPC bridge config space. To enable Intel IGD assignment through vfio-pci, say Y. + +config MLX5_VFIO_PCI + tristate "VFIO support for MLX5 PCI devices" + depends on VFIO_PCI_CORE && MLX5_CORE + help + This provides a generic PCI support for MLX5 devices using the VFIO + framework. + + If you don't know what to do here, say N.
drivers/vfio/pci/Makefile +3 -0
@@ -2,9 +2,12 @@ obj-$(CONFIG_VFIO_PCI_CORE) += vfio-pci-core.o obj-$(CONFIG_VFIO_PCI) += vfio-pci.o +obj-$(CONFIG_MLX5_VFIO_PCI) += mlx5-vfio-pci.o vfio-pci-core-y := vfio_pci_core.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o vfio-pci-core-$(CONFIG_S390) += vfio_pci_zdev.o vfio-pci-y := vfio_pci.o vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o + +mlx5-vfio-pci-y := mlx5_vfio_pci.o
drivers/vfio/pci/mlx5_vfio_pci.c new file +130 -0
130 lines not on this page · raw message
Signature · 1 line
2.21.0
Hi Max/ Yishai,
(Sorry I picked this thread instead of the [1] here as I don't have that in my mailbox)
I see that an update to this series has been posted by Yishai [1] and it mentions about a branch with all relevant patches,
" A preview of all the patches can be seen here:
https://github.com/jgunthorpe/linux/commits/mlx5_vfio_pci"
But sorry I couldn't find the patches in the branch above. Could you please check and let me know.
Thanks,
Shameer
[1] https://lore.kernel.org/kvm/[email protected]/#R
Quoted 101 lines
-----Original Message-----
From: Max Gurtovoy [mailto:[email protected]]
Sent: 03 June 2021 17:08
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Shameerali Kolothum Thodi <[email protected]>; liulongfang <[email protected]>; [email protected]; Max Gurtovoy <[email protected]>
Subject: [RFC PATCH v4 00/11] Introduce vfio-pci-core subsystemHi Alex, Cornelia, Jason and Co,
This series split the
vfio_pcidriver into 2 parts: pci drivers and a subsystem driver that will also be library of code. The main pci driver, vfio_pci.ko will be used as before and it will bind to the subsystem driver vfio_pci_core.ko to register to the VFIO subsystem.This series is coming to solve some of the issues that were raised in the previous attempts for extending vfio-pci for vendor specific functionality:
1. https://lkml.org/lkml/2020/5/17/376 by Yan Zhao.
2. https://www.spinics.net/lists/kernel/msg3903996.html by Longfang LiuThis subsystem framework will also ease on adding new vendor specific functionality to VFIO devices in the future by allowing another module to provide the
pci_driverthat can setup number of details before registering to VFIO subsystem (such as inject its own operations).This series also extends the "
driver_override" mechanism. We added a flag for pci drivers that will declare themselves as "driver_override" capable and only declared drivers can use this mechanism in the PCI subsystem. Other drivers will not be able to bind to devices that use "driver_override". Also, the PCI driver matching will always look for ID table and will never generate dummy "match_all" ID table in the PCI subsystem layer. In this way, we ensure deterministic behaviour with no races with the original pci drivers. In order to get the best match for "driver_override" drivers, one can create a userspace program (example can be found at
https://github.com/maxgurtovoy/linux_tools/blob/main/vfio/bind_vfio_pci_driver.py)
that find the 'best match' according to simple algorithm: "the driver with the fewest '*' matches wins."
For example, the vfio-pci driver will match to any pci device. So it will have the maximal '*' matches (for all matching IDs: vendor, device, subvendor, ...).
In case we are looking for a match to mlx5 based device, we'll have a match to vfio-pci.ko and mlx5-vfio-pci.ko. We'll prefer mlx5-vfio-pci.ko since it will have less '*' matches (probably vendor and device IDs will match). This will work in the future for NVMe/Virtio devices that can match according to a class code or other criteria.The main goal of this series is to agree on the
vfio_pcimodule split and the "driver_override" extensions. The follow-up version will include an extendedmlx5_vfio_pcidriver that will support VF suspend/resume as well.This series applied cleanly on top of vfio reflck re-design (still haven't sent for review) and can be found at:
https://github.com/Mellanox/NVMEoF-P2P/tree/vfio-v4-external.Max Gurtovoy (11):
vfio-pci: renamevfio_pci.ctovfio_pci_core.c
vfio-pci: renamevfio_pci_private.htovfio_pci_core.h
vfio-pci: renamevfio_pci_devicetovfio_pci_core_device
vfio-pci: rename ops functions to fit core namings
vfio-pci: include vfio header invfio_pci_core.h
vfio-pci: introducevfio_pci.c
vfio-pci: move igd initialization tovfio_pci.c
PCI: add flags field topci_device_idstructure
PCI: add matching checks fordriver_overridebinding
vfio-pci: introducevfio_pci_coresubsystem driver
mlx5-vfio-pci: add newvfio_pcidriver for mlx5 devicesDocumentation/ABI/testing/sysfs-bus-pci | 6 +- Documentation/PCI/pci.rst | 1 + drivers/pci/pci-driver.c | 22 +- drivers/vfio/pci/Kconfig | 27 +- drivers/vfio/pci/Makefile | 12 +- drivers/vfio/pci/mlx5_vfio_pci.c | 130 + drivers/vfio/pci/vfio_pci.c | 2329 +---------------- drivers/vfio/pci/vfio_pci_config.c | 70 +- drivers/vfio/pci/vfio_pci_core.c | 2239 ++++++++++++++++ drivers/vfio/pci/vfio_pci_igd.c | 16 +- drivers/vfio/pci/vfio_pci_intrs.c | 42 +- drivers/vfio/pci/vfio_pci_rdwr.c | 18 +- drivers/vfio/pci/vfio_pci_zdev.c | 4 +- include/linux/mod_devicetable.h | 9 + include/linux/pci.h | 27 + .../linux/vfio_pci_core.h | 93 +- scripts/mod/devicetable-offsets.c | 1 + scripts/mod/file2alias.c | 8 +- 18 files changed, 2695 insertions(+), 2359 deletions(-) create mode 100644 drivers/vfio/pci/mlx5_vfio_pci.c create mode 100644 drivers/vfio/pci/vfio_pci_core.c rename drivers/vfio/pci/vfio_pci_private.h => include/linux/vfio_pci_core.h(56%)
--
2.21.0
Quoted 5 lines · Shameerali Kolothum Thodi
Hi Max/ Yishai,
(Sorry I picked this thread instead of the [1] here as I don't have that in my mailbox)
Quoted 8 lines
I see that an update to this series has been posted by Yishai [1] and it mentions about a branch with all relevant patches,
" A preview of all the patches can be seen here:
https://github.com/jgunthorpe/linux/commits/mlx5_vfio_pci"But sorry I couldn't find the patches in the branch above. Could you please check and let me know.
I fixed it
Jason