Re: [Qemu-devel] [PATCH v3] vfio : add aer process

2016-08-18 Thread Zhou Jie

ping

On 2016/8/15 10:53, Zhou Jie wrote:

ping

On 2016/8/2 11:57, Zhou Jie wrote:

During aer err occurs and resume do following to
protect device from being accessed.
1. Make config space read only.
2. Disable INTx/MSI Interrupt.
3. Do nothing for bar regions.

Signed-off-by: Zhou Jie 
---
v2-v3:
   1. Call init_completion() in vfio_pci_probe.
   2. Call reinit_completion() in vfio_pci_aer_err_detected.
   3. Remove unnecessary brackets.

v1-v2:
   1. Add aer process to vfio driver.

 drivers/vfio/pci/vfio_pci.c | 48
+
 drivers/vfio/pci/vfio_pci_private.h |  2 ++
 include/uapi/linux/vfio.h   |  2 ++
 3 files changed, 52 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index d624a52..4c246a1 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -648,6 +648,15 @@ static long vfio_pci_ioctl(void *device_data,
 struct vfio_pci_device *vdev = device_data;
 unsigned long minsz;

+if (vdev->aer_error_in_progress && (cmd == VFIO_DEVICE_SET_IRQS ||
+cmd == VFIO_DEVICE_RESET || cmd == VFIO_DEVICE_PCI_HOT_RESET)) {
+int ret;
+ret = wait_for_completion_interruptible(
+>aer_error_completion);
+if (ret)
+return ret;
+}
+
 if (cmd == VFIO_DEVICE_GET_INFO) {
 struct vfio_device_info info;

@@ -664,6 +673,10 @@ static long vfio_pci_ioctl(void *device_data,
 if (vdev->reset_works)
 info.flags |= VFIO_DEVICE_FLAGS_RESET;

+info.flags |= VFIO_DEVICE_FLAGS_AERPROCESS;
+if (vdev->aer_error_in_progress)
+info.flags |= VFIO_DEVICE_FLAGS_INAERPROCESS;
+
 info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
 info.num_irqs = VFIO_PCI_NUM_IRQS;

@@ -1070,6 +1083,13 @@ static ssize_t vfio_pci_rw(void *device_data,
char __user *buf,

 switch (index) {
 case VFIO_PCI_CONFIG_REGION_INDEX:
+if (vdev->aer_error_in_progress && iswrite) {
+int ret;
+ret = wait_for_completion_interruptible(
+>aer_error_completion);
+if (ret)
+return ret;
+}
 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);

 case VFIO_PCI_ROM_REGION_INDEX:
@@ -1228,6 +1248,7 @@ static int vfio_pci_probe(struct pci_dev *pdev,
const struct pci_device_id *id)
 vdev->irq_type = VFIO_PCI_NUM_IRQS;
 mutex_init(>igate);
 spin_lock_init(>irqlock);
+init_completion(>aer_error_completion);

 ret = vfio_add_group_dev(>dev, _pci_ops, vdev);
 if (ret) {
@@ -1300,6 +1321,11 @@ static pci_ers_result_t
vfio_pci_aer_err_detected(struct pci_dev *pdev,

 mutex_lock(>igate);

+vdev->aer_error_in_progress = true;
+reinit_completion(>aer_error_completion);
+vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
+VFIO_IRQ_SET_ACTION_TRIGGER,
+vdev->irq_type, 0, 0, NULL);
 if (vdev->err_trigger)
 eventfd_signal(vdev->err_trigger, 1);

@@ -1310,8 +1336,30 @@ static pci_ers_result_t
vfio_pci_aer_err_detected(struct pci_dev *pdev,
 return PCI_ERS_RESULT_CAN_RECOVER;
 }

+static void vfio_pci_aer_resume(struct pci_dev *pdev)
+{
+struct vfio_pci_device *vdev;
+struct vfio_device *device;
+
+device = vfio_device_get_from_dev(>dev);
+if (device == NULL)
+return;
+
+vdev = vfio_device_data(device);
+if (vdev == NULL) {
+vfio_device_put(device);
+return;
+}
+
+vdev->aer_error_in_progress = false;
+complete_all(>aer_error_completion);
+
+vfio_device_put(device);
+}
+
 static const struct pci_error_handlers vfio_err_handlers = {
 .error_detected = vfio_pci_aer_err_detected,
+.resume = vfio_pci_aer_resume,
 };

 static struct pci_driver vfio_pci_driver = {
diff --git a/drivers/vfio/pci/vfio_pci_private.h
b/drivers/vfio/pci/vfio_pci_private.h
index 2128de8..7430d92 100644
--- a/drivers/vfio/pci/vfio_pci_private.h
+++ b/drivers/vfio/pci/vfio_pci_private.h
@@ -91,6 +91,8 @@ struct vfio_pci_device {
 boolhas_vga;
 boolneeds_reset;
 boolnointx;
+boolaer_error_in_progress;
+struct completionaer_error_completion;
 struct pci_saved_state*pci_saved_state;
 intrefcnt;
 struct eventfd_ctx*err_trigger;
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 255a211..59b9cf6 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -198,6 +198,8 @@ struct vfio_device_info {
 #define VFIO_DEVICE_FLAGS_PCI(1 << 1)/* vfio-pci device */
 #define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2)/* vfio-platform
device */
 #define VFIO_DEVICE_FLAGS_AMBA  (1 << 3)/* vfio-amba device */
+#define VFIO_DEVICE_FLAGS_AERPROCESS  (1 << 4)/* support aer
error progress */
+#define VFIO_DEVICE_FLAGS_INAERPROCESS  (1 << 5)/* 

Re: [Qemu-devel] [PATCH v3] vfio : add aer process

2016-08-18 Thread Zhou Jie

ping

On 2016/8/15 10:53, Zhou Jie wrote:

ping

On 2016/8/2 11:57, Zhou Jie wrote:

During aer err occurs and resume do following to
protect device from being accessed.
1. Make config space read only.
2. Disable INTx/MSI Interrupt.
3. Do nothing for bar regions.

Signed-off-by: Zhou Jie 
---
v2-v3:
   1. Call init_completion() in vfio_pci_probe.
   2. Call reinit_completion() in vfio_pci_aer_err_detected.
   3. Remove unnecessary brackets.

v1-v2:
   1. Add aer process to vfio driver.

 drivers/vfio/pci/vfio_pci.c | 48
+
 drivers/vfio/pci/vfio_pci_private.h |  2 ++
 include/uapi/linux/vfio.h   |  2 ++
 3 files changed, 52 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index d624a52..4c246a1 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -648,6 +648,15 @@ static long vfio_pci_ioctl(void *device_data,
 struct vfio_pci_device *vdev = device_data;
 unsigned long minsz;

+if (vdev->aer_error_in_progress && (cmd == VFIO_DEVICE_SET_IRQS ||
+cmd == VFIO_DEVICE_RESET || cmd == VFIO_DEVICE_PCI_HOT_RESET)) {
+int ret;
+ret = wait_for_completion_interruptible(
+>aer_error_completion);
+if (ret)
+return ret;
+}
+
 if (cmd == VFIO_DEVICE_GET_INFO) {
 struct vfio_device_info info;

@@ -664,6 +673,10 @@ static long vfio_pci_ioctl(void *device_data,
 if (vdev->reset_works)
 info.flags |= VFIO_DEVICE_FLAGS_RESET;

+info.flags |= VFIO_DEVICE_FLAGS_AERPROCESS;
+if (vdev->aer_error_in_progress)
+info.flags |= VFIO_DEVICE_FLAGS_INAERPROCESS;
+
 info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
 info.num_irqs = VFIO_PCI_NUM_IRQS;

@@ -1070,6 +1083,13 @@ static ssize_t vfio_pci_rw(void *device_data,
char __user *buf,

 switch (index) {
 case VFIO_PCI_CONFIG_REGION_INDEX:
+if (vdev->aer_error_in_progress && iswrite) {
+int ret;
+ret = wait_for_completion_interruptible(
+>aer_error_completion);
+if (ret)
+return ret;
+}
 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);

 case VFIO_PCI_ROM_REGION_INDEX:
@@ -1228,6 +1248,7 @@ static int vfio_pci_probe(struct pci_dev *pdev,
const struct pci_device_id *id)
 vdev->irq_type = VFIO_PCI_NUM_IRQS;
 mutex_init(>igate);
 spin_lock_init(>irqlock);
+init_completion(>aer_error_completion);

 ret = vfio_add_group_dev(>dev, _pci_ops, vdev);
 if (ret) {
@@ -1300,6 +1321,11 @@ static pci_ers_result_t
vfio_pci_aer_err_detected(struct pci_dev *pdev,

 mutex_lock(>igate);

+vdev->aer_error_in_progress = true;
+reinit_completion(>aer_error_completion);
+vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
+VFIO_IRQ_SET_ACTION_TRIGGER,
+vdev->irq_type, 0, 0, NULL);
 if (vdev->err_trigger)
 eventfd_signal(vdev->err_trigger, 1);

@@ -1310,8 +1336,30 @@ static pci_ers_result_t
vfio_pci_aer_err_detected(struct pci_dev *pdev,
 return PCI_ERS_RESULT_CAN_RECOVER;
 }

+static void vfio_pci_aer_resume(struct pci_dev *pdev)
+{
+struct vfio_pci_device *vdev;
+struct vfio_device *device;
+
+device = vfio_device_get_from_dev(>dev);
+if (device == NULL)
+return;
+
+vdev = vfio_device_data(device);
+if (vdev == NULL) {
+vfio_device_put(device);
+return;
+}
+
+vdev->aer_error_in_progress = false;
+complete_all(>aer_error_completion);
+
+vfio_device_put(device);
+}
+
 static const struct pci_error_handlers vfio_err_handlers = {
 .error_detected = vfio_pci_aer_err_detected,
+.resume = vfio_pci_aer_resume,
 };

 static struct pci_driver vfio_pci_driver = {
diff --git a/drivers/vfio/pci/vfio_pci_private.h
b/drivers/vfio/pci/vfio_pci_private.h
index 2128de8..7430d92 100644
--- a/drivers/vfio/pci/vfio_pci_private.h
+++ b/drivers/vfio/pci/vfio_pci_private.h
@@ -91,6 +91,8 @@ struct vfio_pci_device {
 boolhas_vga;
 boolneeds_reset;
 boolnointx;
+boolaer_error_in_progress;
+struct completionaer_error_completion;
 struct pci_saved_state*pci_saved_state;
 intrefcnt;
 struct eventfd_ctx*err_trigger;
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 255a211..59b9cf6 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -198,6 +198,8 @@ struct vfio_device_info {
 #define VFIO_DEVICE_FLAGS_PCI(1 << 1)/* vfio-pci device */
 #define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2)/* vfio-platform
device */
 #define VFIO_DEVICE_FLAGS_AMBA  (1 << 3)/* vfio-amba device */
+#define VFIO_DEVICE_FLAGS_AERPROCESS  (1 << 4)/* support aer
error progress */
+#define VFIO_DEVICE_FLAGS_INAERPROCESS  (1 << 5)/* status in aer
error progress 

Re: [PATCH v3] vfio : add aer process

2016-08-14 Thread Zhou Jie

ping

On 2016/8/2 11:57, Zhou Jie wrote:

During aer err occurs and resume do following to
protect device from being accessed.
1. Make config space read only.
2. Disable INTx/MSI Interrupt.
3. Do nothing for bar regions.

Signed-off-by: Zhou Jie 
---
v2-v3:
   1. Call init_completion() in vfio_pci_probe.
   2. Call reinit_completion() in vfio_pci_aer_err_detected.
   3. Remove unnecessary brackets.

v1-v2:
   1. Add aer process to vfio driver.

 drivers/vfio/pci/vfio_pci.c | 48 +
 drivers/vfio/pci/vfio_pci_private.h |  2 ++
 include/uapi/linux/vfio.h   |  2 ++
 3 files changed, 52 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index d624a52..4c246a1 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -648,6 +648,15 @@ static long vfio_pci_ioctl(void *device_data,
struct vfio_pci_device *vdev = device_data;
unsigned long minsz;

+   if (vdev->aer_error_in_progress && (cmd == VFIO_DEVICE_SET_IRQS ||
+   cmd == VFIO_DEVICE_RESET || cmd == VFIO_DEVICE_PCI_HOT_RESET)) {
+   int ret;
+   ret = wait_for_completion_interruptible(
+   >aer_error_completion);
+   if (ret)
+   return ret;
+   }
+
if (cmd == VFIO_DEVICE_GET_INFO) {
struct vfio_device_info info;

@@ -664,6 +673,10 @@ static long vfio_pci_ioctl(void *device_data,
if (vdev->reset_works)
info.flags |= VFIO_DEVICE_FLAGS_RESET;

+   info.flags |= VFIO_DEVICE_FLAGS_AERPROCESS;
+   if (vdev->aer_error_in_progress)
+   info.flags |= VFIO_DEVICE_FLAGS_INAERPROCESS;
+
info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
info.num_irqs = VFIO_PCI_NUM_IRQS;

@@ -1070,6 +1083,13 @@ static ssize_t vfio_pci_rw(void *device_data, char 
__user *buf,

switch (index) {
case VFIO_PCI_CONFIG_REGION_INDEX:
+   if (vdev->aer_error_in_progress && iswrite) {
+   int ret;
+   ret = wait_for_completion_interruptible(
+   >aer_error_completion);
+   if (ret)
+   return ret;
+   }
return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);

case VFIO_PCI_ROM_REGION_INDEX:
@@ -1228,6 +1248,7 @@ static int vfio_pci_probe(struct pci_dev *pdev, const 
struct pci_device_id *id)
vdev->irq_type = VFIO_PCI_NUM_IRQS;
mutex_init(>igate);
spin_lock_init(>irqlock);
+   init_completion(>aer_error_completion);

ret = vfio_add_group_dev(>dev, _pci_ops, vdev);
if (ret) {
@@ -1300,6 +1321,11 @@ static pci_ers_result_t vfio_pci_aer_err_detected(struct 
pci_dev *pdev,

mutex_lock(>igate);

+   vdev->aer_error_in_progress = true;
+   reinit_completion(>aer_error_completion);
+   vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
+   VFIO_IRQ_SET_ACTION_TRIGGER,
+   vdev->irq_type, 0, 0, NULL);
if (vdev->err_trigger)
eventfd_signal(vdev->err_trigger, 1);

@@ -1310,8 +1336,30 @@ static pci_ers_result_t vfio_pci_aer_err_detected(struct 
pci_dev *pdev,
return PCI_ERS_RESULT_CAN_RECOVER;
 }

+static void vfio_pci_aer_resume(struct pci_dev *pdev)
+{
+   struct vfio_pci_device *vdev;
+   struct vfio_device *device;
+
+   device = vfio_device_get_from_dev(>dev);
+   if (device == NULL)
+   return;
+
+   vdev = vfio_device_data(device);
+   if (vdev == NULL) {
+   vfio_device_put(device);
+   return;
+   }
+
+   vdev->aer_error_in_progress = false;
+   complete_all(>aer_error_completion);
+
+   vfio_device_put(device);
+}
+
 static const struct pci_error_handlers vfio_err_handlers = {
.error_detected = vfio_pci_aer_err_detected,
+   .resume = vfio_pci_aer_resume,
 };

 static struct pci_driver vfio_pci_driver = {
diff --git a/drivers/vfio/pci/vfio_pci_private.h 
b/drivers/vfio/pci/vfio_pci_private.h
index 2128de8..7430d92 100644
--- a/drivers/vfio/pci/vfio_pci_private.h
+++ b/drivers/vfio/pci/vfio_pci_private.h
@@ -91,6 +91,8 @@ struct vfio_pci_device {
boolhas_vga;
boolneeds_reset;
boolnointx;
+   boolaer_error_in_progress;
+   struct completion   aer_error_completion;
struct pci_saved_state  *pci_saved_state;
int refcnt;
struct eventfd_ctx  *err_trigger;
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 255a211..59b9cf6 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -198,6 +198,8 

Re: [PATCH v3] vfio : add aer process

2016-08-14 Thread Zhou Jie

ping

On 2016/8/2 11:57, Zhou Jie wrote:

During aer err occurs and resume do following to
protect device from being accessed.
1. Make config space read only.
2. Disable INTx/MSI Interrupt.
3. Do nothing for bar regions.

Signed-off-by: Zhou Jie 
---
v2-v3:
   1. Call init_completion() in vfio_pci_probe.
   2. Call reinit_completion() in vfio_pci_aer_err_detected.
   3. Remove unnecessary brackets.

v1-v2:
   1. Add aer process to vfio driver.

 drivers/vfio/pci/vfio_pci.c | 48 +
 drivers/vfio/pci/vfio_pci_private.h |  2 ++
 include/uapi/linux/vfio.h   |  2 ++
 3 files changed, 52 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index d624a52..4c246a1 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -648,6 +648,15 @@ static long vfio_pci_ioctl(void *device_data,
struct vfio_pci_device *vdev = device_data;
unsigned long minsz;

+   if (vdev->aer_error_in_progress && (cmd == VFIO_DEVICE_SET_IRQS ||
+   cmd == VFIO_DEVICE_RESET || cmd == VFIO_DEVICE_PCI_HOT_RESET)) {
+   int ret;
+   ret = wait_for_completion_interruptible(
+   >aer_error_completion);
+   if (ret)
+   return ret;
+   }
+
if (cmd == VFIO_DEVICE_GET_INFO) {
struct vfio_device_info info;

@@ -664,6 +673,10 @@ static long vfio_pci_ioctl(void *device_data,
if (vdev->reset_works)
info.flags |= VFIO_DEVICE_FLAGS_RESET;

+   info.flags |= VFIO_DEVICE_FLAGS_AERPROCESS;
+   if (vdev->aer_error_in_progress)
+   info.flags |= VFIO_DEVICE_FLAGS_INAERPROCESS;
+
info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
info.num_irqs = VFIO_PCI_NUM_IRQS;

@@ -1070,6 +1083,13 @@ static ssize_t vfio_pci_rw(void *device_data, char 
__user *buf,

switch (index) {
case VFIO_PCI_CONFIG_REGION_INDEX:
+   if (vdev->aer_error_in_progress && iswrite) {
+   int ret;
+   ret = wait_for_completion_interruptible(
+   >aer_error_completion);
+   if (ret)
+   return ret;
+   }
return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);

case VFIO_PCI_ROM_REGION_INDEX:
@@ -1228,6 +1248,7 @@ static int vfio_pci_probe(struct pci_dev *pdev, const 
struct pci_device_id *id)
vdev->irq_type = VFIO_PCI_NUM_IRQS;
mutex_init(>igate);
spin_lock_init(>irqlock);
+   init_completion(>aer_error_completion);

ret = vfio_add_group_dev(>dev, _pci_ops, vdev);
if (ret) {
@@ -1300,6 +1321,11 @@ static pci_ers_result_t vfio_pci_aer_err_detected(struct 
pci_dev *pdev,

mutex_lock(>igate);

+   vdev->aer_error_in_progress = true;
+   reinit_completion(>aer_error_completion);
+   vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
+   VFIO_IRQ_SET_ACTION_TRIGGER,
+   vdev->irq_type, 0, 0, NULL);
if (vdev->err_trigger)
eventfd_signal(vdev->err_trigger, 1);

@@ -1310,8 +1336,30 @@ static pci_ers_result_t vfio_pci_aer_err_detected(struct 
pci_dev *pdev,
return PCI_ERS_RESULT_CAN_RECOVER;
 }

+static void vfio_pci_aer_resume(struct pci_dev *pdev)
+{
+   struct vfio_pci_device *vdev;
+   struct vfio_device *device;
+
+   device = vfio_device_get_from_dev(>dev);
+   if (device == NULL)
+   return;
+
+   vdev = vfio_device_data(device);
+   if (vdev == NULL) {
+   vfio_device_put(device);
+   return;
+   }
+
+   vdev->aer_error_in_progress = false;
+   complete_all(>aer_error_completion);
+
+   vfio_device_put(device);
+}
+
 static const struct pci_error_handlers vfio_err_handlers = {
.error_detected = vfio_pci_aer_err_detected,
+   .resume = vfio_pci_aer_resume,
 };

 static struct pci_driver vfio_pci_driver = {
diff --git a/drivers/vfio/pci/vfio_pci_private.h 
b/drivers/vfio/pci/vfio_pci_private.h
index 2128de8..7430d92 100644
--- a/drivers/vfio/pci/vfio_pci_private.h
+++ b/drivers/vfio/pci/vfio_pci_private.h
@@ -91,6 +91,8 @@ struct vfio_pci_device {
boolhas_vga;
boolneeds_reset;
boolnointx;
+   boolaer_error_in_progress;
+   struct completion   aer_error_completion;
struct pci_saved_state  *pci_saved_state;
int refcnt;
struct eventfd_ctx  *err_trigger;
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 255a211..59b9cf6 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -198,6 +198,8 @@ struct vfio_device_info 

[PATCH v3] vfio : add aer process

2016-08-01 Thread Zhou Jie
During aer err occurs and resume do following to
protect device from being accessed.
1. Make config space read only.
2. Disable INTx/MSI Interrupt.
3. Do nothing for bar regions.

Signed-off-by: Zhou Jie 
---
v2-v3:
   1. Call init_completion() in vfio_pci_probe.
   2. Call reinit_completion() in vfio_pci_aer_err_detected.
   3. Remove unnecessary brackets.

v1-v2:
   1. Add aer process to vfio driver.

 drivers/vfio/pci/vfio_pci.c | 48 +
 drivers/vfio/pci/vfio_pci_private.h |  2 ++
 include/uapi/linux/vfio.h   |  2 ++
 3 files changed, 52 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index d624a52..4c246a1 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -648,6 +648,15 @@ static long vfio_pci_ioctl(void *device_data,
struct vfio_pci_device *vdev = device_data;
unsigned long minsz;
 
+   if (vdev->aer_error_in_progress && (cmd == VFIO_DEVICE_SET_IRQS ||
+   cmd == VFIO_DEVICE_RESET || cmd == VFIO_DEVICE_PCI_HOT_RESET)) {
+   int ret;
+   ret = wait_for_completion_interruptible(
+   >aer_error_completion);
+   if (ret)
+   return ret;
+   }
+
if (cmd == VFIO_DEVICE_GET_INFO) {
struct vfio_device_info info;
 
@@ -664,6 +673,10 @@ static long vfio_pci_ioctl(void *device_data,
if (vdev->reset_works)
info.flags |= VFIO_DEVICE_FLAGS_RESET;
 
+   info.flags |= VFIO_DEVICE_FLAGS_AERPROCESS;
+   if (vdev->aer_error_in_progress)
+   info.flags |= VFIO_DEVICE_FLAGS_INAERPROCESS;
+
info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
info.num_irqs = VFIO_PCI_NUM_IRQS;
 
@@ -1070,6 +1083,13 @@ static ssize_t vfio_pci_rw(void *device_data, char 
__user *buf,
 
switch (index) {
case VFIO_PCI_CONFIG_REGION_INDEX:
+   if (vdev->aer_error_in_progress && iswrite) {
+   int ret;
+   ret = wait_for_completion_interruptible(
+   >aer_error_completion);
+   if (ret)
+   return ret;
+   }
return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
 
case VFIO_PCI_ROM_REGION_INDEX:
@@ -1228,6 +1248,7 @@ static int vfio_pci_probe(struct pci_dev *pdev, const 
struct pci_device_id *id)
vdev->irq_type = VFIO_PCI_NUM_IRQS;
mutex_init(>igate);
spin_lock_init(>irqlock);
+   init_completion(>aer_error_completion);
 
ret = vfio_add_group_dev(>dev, _pci_ops, vdev);
if (ret) {
@@ -1300,6 +1321,11 @@ static pci_ers_result_t vfio_pci_aer_err_detected(struct 
pci_dev *pdev,
 
mutex_lock(>igate);
 
+   vdev->aer_error_in_progress = true;
+   reinit_completion(>aer_error_completion);
+   vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
+   VFIO_IRQ_SET_ACTION_TRIGGER,
+   vdev->irq_type, 0, 0, NULL);
if (vdev->err_trigger)
eventfd_signal(vdev->err_trigger, 1);
 
@@ -1310,8 +1336,30 @@ static pci_ers_result_t vfio_pci_aer_err_detected(struct 
pci_dev *pdev,
return PCI_ERS_RESULT_CAN_RECOVER;
 }
 
+static void vfio_pci_aer_resume(struct pci_dev *pdev)
+{
+   struct vfio_pci_device *vdev;
+   struct vfio_device *device;
+
+   device = vfio_device_get_from_dev(>dev);
+   if (device == NULL)
+   return;
+
+   vdev = vfio_device_data(device);
+   if (vdev == NULL) {
+   vfio_device_put(device);
+   return;
+   }
+
+   vdev->aer_error_in_progress = false;
+   complete_all(>aer_error_completion);
+
+   vfio_device_put(device);
+}
+
 static const struct pci_error_handlers vfio_err_handlers = {
.error_detected = vfio_pci_aer_err_detected,
+   .resume = vfio_pci_aer_resume,
 };
 
 static struct pci_driver vfio_pci_driver = {
diff --git a/drivers/vfio/pci/vfio_pci_private.h 
b/drivers/vfio/pci/vfio_pci_private.h
index 2128de8..7430d92 100644
--- a/drivers/vfio/pci/vfio_pci_private.h
+++ b/drivers/vfio/pci/vfio_pci_private.h
@@ -91,6 +91,8 @@ struct vfio_pci_device {
boolhas_vga;
boolneeds_reset;
boolnointx;
+   boolaer_error_in_progress;
+   struct completion   aer_error_completion;
struct pci_saved_state  *pci_saved_state;
int refcnt;
struct eventfd_ctx  *err_trigger;
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 255a211..59b9cf6 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -198,6 +198,8 @@ struct vfio_device_info {
 

[PATCH v3] vfio : add aer process

2016-08-01 Thread Zhou Jie
During aer err occurs and resume do following to
protect device from being accessed.
1. Make config space read only.
2. Disable INTx/MSI Interrupt.
3. Do nothing for bar regions.

Signed-off-by: Zhou Jie 
---
v2-v3:
   1. Call init_completion() in vfio_pci_probe.
   2. Call reinit_completion() in vfio_pci_aer_err_detected.
   3. Remove unnecessary brackets.

v1-v2:
   1. Add aer process to vfio driver.

 drivers/vfio/pci/vfio_pci.c | 48 +
 drivers/vfio/pci/vfio_pci_private.h |  2 ++
 include/uapi/linux/vfio.h   |  2 ++
 3 files changed, 52 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index d624a52..4c246a1 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -648,6 +648,15 @@ static long vfio_pci_ioctl(void *device_data,
struct vfio_pci_device *vdev = device_data;
unsigned long minsz;
 
+   if (vdev->aer_error_in_progress && (cmd == VFIO_DEVICE_SET_IRQS ||
+   cmd == VFIO_DEVICE_RESET || cmd == VFIO_DEVICE_PCI_HOT_RESET)) {
+   int ret;
+   ret = wait_for_completion_interruptible(
+   >aer_error_completion);
+   if (ret)
+   return ret;
+   }
+
if (cmd == VFIO_DEVICE_GET_INFO) {
struct vfio_device_info info;
 
@@ -664,6 +673,10 @@ static long vfio_pci_ioctl(void *device_data,
if (vdev->reset_works)
info.flags |= VFIO_DEVICE_FLAGS_RESET;
 
+   info.flags |= VFIO_DEVICE_FLAGS_AERPROCESS;
+   if (vdev->aer_error_in_progress)
+   info.flags |= VFIO_DEVICE_FLAGS_INAERPROCESS;
+
info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
info.num_irqs = VFIO_PCI_NUM_IRQS;
 
@@ -1070,6 +1083,13 @@ static ssize_t vfio_pci_rw(void *device_data, char 
__user *buf,
 
switch (index) {
case VFIO_PCI_CONFIG_REGION_INDEX:
+   if (vdev->aer_error_in_progress && iswrite) {
+   int ret;
+   ret = wait_for_completion_interruptible(
+   >aer_error_completion);
+   if (ret)
+   return ret;
+   }
return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
 
case VFIO_PCI_ROM_REGION_INDEX:
@@ -1228,6 +1248,7 @@ static int vfio_pci_probe(struct pci_dev *pdev, const 
struct pci_device_id *id)
vdev->irq_type = VFIO_PCI_NUM_IRQS;
mutex_init(>igate);
spin_lock_init(>irqlock);
+   init_completion(>aer_error_completion);
 
ret = vfio_add_group_dev(>dev, _pci_ops, vdev);
if (ret) {
@@ -1300,6 +1321,11 @@ static pci_ers_result_t vfio_pci_aer_err_detected(struct 
pci_dev *pdev,
 
mutex_lock(>igate);
 
+   vdev->aer_error_in_progress = true;
+   reinit_completion(>aer_error_completion);
+   vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
+   VFIO_IRQ_SET_ACTION_TRIGGER,
+   vdev->irq_type, 0, 0, NULL);
if (vdev->err_trigger)
eventfd_signal(vdev->err_trigger, 1);
 
@@ -1310,8 +1336,30 @@ static pci_ers_result_t vfio_pci_aer_err_detected(struct 
pci_dev *pdev,
return PCI_ERS_RESULT_CAN_RECOVER;
 }
 
+static void vfio_pci_aer_resume(struct pci_dev *pdev)
+{
+   struct vfio_pci_device *vdev;
+   struct vfio_device *device;
+
+   device = vfio_device_get_from_dev(>dev);
+   if (device == NULL)
+   return;
+
+   vdev = vfio_device_data(device);
+   if (vdev == NULL) {
+   vfio_device_put(device);
+   return;
+   }
+
+   vdev->aer_error_in_progress = false;
+   complete_all(>aer_error_completion);
+
+   vfio_device_put(device);
+}
+
 static const struct pci_error_handlers vfio_err_handlers = {
.error_detected = vfio_pci_aer_err_detected,
+   .resume = vfio_pci_aer_resume,
 };
 
 static struct pci_driver vfio_pci_driver = {
diff --git a/drivers/vfio/pci/vfio_pci_private.h 
b/drivers/vfio/pci/vfio_pci_private.h
index 2128de8..7430d92 100644
--- a/drivers/vfio/pci/vfio_pci_private.h
+++ b/drivers/vfio/pci/vfio_pci_private.h
@@ -91,6 +91,8 @@ struct vfio_pci_device {
boolhas_vga;
boolneeds_reset;
boolnointx;
+   boolaer_error_in_progress;
+   struct completion   aer_error_completion;
struct pci_saved_state  *pci_saved_state;
int refcnt;
struct eventfd_ctx  *err_trigger;
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 255a211..59b9cf6 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -198,6 +198,8 @@ struct vfio_device_info {
 #define