diff mbox

[v8,3/3] tcm_vhost: Add ioctl to get and set events missed flag

Message ID 1366774344-11127-4-git-send-email-asias@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Asias He April 24, 2013, 3:32 a.m. UTC
Signed-off-by: Asias He <asias@redhat.com>
---
 drivers/vhost/tcm_vhost.c | 16 ++++++++++++++++
 drivers/vhost/tcm_vhost.h |  3 +++
 2 files changed, 19 insertions(+)

Comments

Michael S. Tsirkin April 24, 2013, 9:33 a.m. UTC | #1
On Wed, Apr 24, 2013 at 11:32:24AM +0800, Asias He wrote:
> Signed-off-by: Asias He <asias@redhat.com>

Looks like a good idea to me. I missed this patch
previously, thanks for addressing this.
Some implementation comments below.

> ---
>  drivers/vhost/tcm_vhost.c | 16 ++++++++++++++++
>  drivers/vhost/tcm_vhost.h |  3 +++
>  2 files changed, 19 insertions(+)
> 
> diff --git a/drivers/vhost/tcm_vhost.c b/drivers/vhost/tcm_vhost.c
> index def0c57..11fca73 100644
> --- a/drivers/vhost/tcm_vhost.c
> +++ b/drivers/vhost/tcm_vhost.c
> @@ -1204,6 +1204,8 @@ static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
>  	u64 __user *featurep = argp;
>  	u64 features;
>  	int r, abi_version = VHOST_SCSI_ABI_VERSION;
> +	struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
> +	bool events_missed;
>  
>  	switch (ioctl) {
>  	case VHOST_SCSI_SET_ENDPOINT:
> @@ -1224,6 +1226,20 @@ static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
>  		if (copy_to_user(argp, &abi_version, sizeof abi_version))
>  			return -EFAULT;
>  		return 0;
> +	case VHOST_SCSI_SET_EVENTS_MISSED:
> +		if (copy_from_user(&events_missed, argp, sizeof(events_missed)))
> +			return -EFAULT;

This can trigger undefined behaviour because userspace can
make bool have values other than 0 or 1.
So please read an integer here.
This also let you use get_user.

> +		mutex_lock(&vq->mutex);
> +		vs->vs_events_missed = events_missed;
> +		mutex_unlock(&vq->mutex);
> +		return 0;
> +	case VHOST_SCSI_GET_EVENTS_MISSED:
> +		mutex_lock(&vq->mutex);
> +		events_missed = vs->vs_events_missed;
> +		mutex_unlock(&vq->mutex);
> +		if (copy_to_user(argp, &events_missed, sizeof(events_missed)))
> +			return -EFAULT;
> +		return 0;
>  	case VHOST_GET_FEATURES:
>  		features = VHOST_SCSI_FEATURES;
>  		if (copy_to_user(featurep, &features, sizeof features))
> diff --git a/drivers/vhost/tcm_vhost.h b/drivers/vhost/tcm_vhost.h
> index 94e9ee53..b473e25 100644
> --- a/drivers/vhost/tcm_vhost.h
> +++ b/drivers/vhost/tcm_vhost.h
> @@ -123,3 +123,6 @@ struct vhost_scsi_target {
>  #define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
>  /* Changing this breaks userspace. */
>  #define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int)
> +/* Set and get the events missed flag */
> +#define VHOST_SCSI_SET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x43, bool)
> +#define VHOST_SCSI_GET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x44, bool)

Please don't do bool in user interfaces. No one does. Make it __u32.

> -- 
> 1.8.1.4
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/vhost/tcm_vhost.c b/drivers/vhost/tcm_vhost.c
index def0c57..11fca73 100644
--- a/drivers/vhost/tcm_vhost.c
+++ b/drivers/vhost/tcm_vhost.c
@@ -1204,6 +1204,8 @@  static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
 	u64 __user *featurep = argp;
 	u64 features;
 	int r, abi_version = VHOST_SCSI_ABI_VERSION;
+	struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
+	bool events_missed;
 
 	switch (ioctl) {
 	case VHOST_SCSI_SET_ENDPOINT:
@@ -1224,6 +1226,20 @@  static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
 		if (copy_to_user(argp, &abi_version, sizeof abi_version))
 			return -EFAULT;
 		return 0;
+	case VHOST_SCSI_SET_EVENTS_MISSED:
+		if (copy_from_user(&events_missed, argp, sizeof(events_missed)))
+			return -EFAULT;
+		mutex_lock(&vq->mutex);
+		vs->vs_events_missed = events_missed;
+		mutex_unlock(&vq->mutex);
+		return 0;
+	case VHOST_SCSI_GET_EVENTS_MISSED:
+		mutex_lock(&vq->mutex);
+		events_missed = vs->vs_events_missed;
+		mutex_unlock(&vq->mutex);
+		if (copy_to_user(argp, &events_missed, sizeof(events_missed)))
+			return -EFAULT;
+		return 0;
 	case VHOST_GET_FEATURES:
 		features = VHOST_SCSI_FEATURES;
 		if (copy_to_user(featurep, &features, sizeof features))
diff --git a/drivers/vhost/tcm_vhost.h b/drivers/vhost/tcm_vhost.h
index 94e9ee53..b473e25 100644
--- a/drivers/vhost/tcm_vhost.h
+++ b/drivers/vhost/tcm_vhost.h
@@ -123,3 +123,6 @@  struct vhost_scsi_target {
 #define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
 /* Changing this breaks userspace. */
 #define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int)
+/* Set and get the events missed flag */
+#define VHOST_SCSI_SET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x43, bool)
+#define VHOST_SCSI_GET_EVENTS_MISSED _IOW(VHOST_VIRTIO, 0x44, bool)