[PATCH] drm: Honour O_NONBLOCK on the device for reading events

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Otherwise drmHandleEvent will block if accidentally read too often. In
order to handle the case where the event is read off the queue by
another thread before we dequeue the event, we delay the actual checking
for EAGAIN to under the spinlock and so inline drm_dequeue_event().

Signed-off-by: Chris Wilson <chris@xxxxxxxxxxxxxxxxxx>
---
v3: Compilation. Remember to at least test changes before hitting send.
-Chris
---
 drivers/gpu/drm/drm_fops.c |   79 +++++++++++++++++++++-----------------------
 1 files changed, 38 insertions(+), 41 deletions(-)

diff --git a/drivers/gpu/drm/drm_fops.c b/drivers/gpu/drm/drm_fops.c
index 2ec7d48..8efc07f 100644
--- a/drivers/gpu/drm/drm_fops.c
+++ b/drivers/gpu/drm/drm_fops.c
@@ -580,61 +580,58 @@ int drm_release(struct inode *inode, struct file *filp)
 }
 EXPORT_SYMBOL(drm_release);
 
-static bool
-drm_dequeue_event(struct drm_file *file_priv,
-		  size_t total, size_t max, struct drm_pending_event **out)
+ssize_t drm_read(struct file *filp, char __user *buffer,
+		 size_t count, loff_t *offset)
 {
+	struct drm_file *file_priv = filp->private_data;
 	struct drm_device *dev = file_priv->minor->dev;
 	struct drm_pending_event *e;
 	unsigned long flags;
-	bool ret = false;
+	ssize_t ret;
+	int err;
 
-	spin_lock_irqsave(&dev->event_lock, flags);
+	if ((filp->f_flags & O_NONBLOCK) == 0) {
+		ret = wait_event_interruptible(file_priv->event_wait,
+					       !list_empty(&file_priv->event_list));
+		if (ret < 0)
+			return ret;
+	}
 
-	*out = NULL;
-	if (list_empty(&file_priv->event_list))
-		goto out;
-	e = list_first_entry(&file_priv->event_list,
-			     struct drm_pending_event, link);
-	if (e->event->length + total > max)
-		goto out;
+	ret = err = 0;
+	spin_lock_irqsave(&dev->event_lock, flags);
+	do {
+		if (list_empty(&file_priv->event_list)) {
+			if (filp->f_flags & O_NONBLOCK)
+				err = -EAGAIN;
+			goto unlock;
+		}
 
-	file_priv->event_space += e->event->length;
-	list_del(&e->link);
-	*out = e;
-	ret = true;
+		e = list_first_entry(&file_priv->event_list,
+				     struct drm_pending_event, link);
+		if (e->event->length + ret > count) {
+			err = -EINVAL;
+			goto unlock;
+		}
 
-out:
-	spin_unlock_irqrestore(&dev->event_lock, flags);
-	return ret;
-}
+		file_priv->event_space += e->event->length;
+		list_del(&e->link);
 
-ssize_t drm_read(struct file *filp, char __user *buffer,
-		 size_t count, loff_t *offset)
-{
-	struct drm_file *file_priv = filp->private_data;
-	struct drm_pending_event *e;
-	size_t total;
-	ssize_t ret;
+		spin_unlock_irqrestore(&dev->event_lock, flags);
 
-	ret = wait_event_interruptible(file_priv->event_wait,
-				       !list_empty(&file_priv->event_list));
-	if (ret < 0)
-		return ret;
-
-	total = 0;
-	while (drm_dequeue_event(file_priv, total, count, &e)) {
-		if (copy_to_user(buffer + total,
-				 e->event, e->event->length)) {
-			total = -EFAULT;
-			break;
+		if (copy_to_user(buffer + ret, e->event, e->event->length)) {
+			err = -EFAULT;
+			goto out;
 		}
 
-		total += e->event->length;
+		ret += e->event->length;
 		e->destroy(e);
-	}
 
-	return total;
+		spin_lock_irqsave(&dev->event_lock, flags);
+	} while (1);
+unlock:
+	spin_unlock_irqrestore(&dev->event_lock, flags);
+out:
+	return ret ? ret : err;
 }
 EXPORT_SYMBOL(drm_read);
 
-- 
1.7.5.4

_______________________________________________
dri-devel mailing list
dri-devel@xxxxxxxxxxxxxxxxxxxxx
http://lists.freedesktop.org/mailman/listinfo/dri-devel


[Index of Archives]     [Linux DRI Users]     [Linux Intel Graphics]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux