Skip to contentSkip to conversation

xenbus: avoid zero returns from read()

PATCH In review Daniel De Graaf 3 participants 5 messages Sep 8, 2010 – Dec 30, 2010 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xenbus: avoid zero returns from read()

It is possible to get a zero return from read() in instances where the queue is not empty but has no elements with data to deliver to the user. Since a zero return from read is an error indicator, resume waiting or return -EAGAIN (for a nonblocking fd) in this case.

---

drivers/xen/xenfs/xenbus.c +3 -0
index 88c87c9..0ddef43 100644
@@ -121,6 +121,7 @@ static ssize_t xenbus_file_read(struct file *filp,
 	int ret;
 
 	mutex_lock(&u->reply_mutex);
+again:
 	while (list_empty(&u->read_buffers)) {
 		mutex_unlock(&u->reply_mutex);
 		if (filp->f_flags & O_NONBLOCK)
@@ -159,6 +160,8 @@ static ssize_t xenbus_file_read(struct file *filp,
 					struct read_buffer, list);
 		}
 	}
+	if (i == 0)
+		goto again;
 
 out:
 	mutex_unlock(&u->reply_mutex);
#1 lore raw

Is it related to the following patch? The following patch fixes the problem of queue deletion.

drivers/xen/xenfs/xenbus.c +1 -1
index 64b3be4..763e90d 100644
@@ -143,7 +143,7 @@ static ssize_t xenbus_file_read(struct file *filp,
                i += sz - ret;
                rb->cons += sz - ret;
 
-               if (ret != sz) {
+               if (ret != 0) {
                        if (i == 0)
                                i = -EFAULT;
                        goto out;

Jun Zhu
Citrix Systems UK
________________________________________
From: Daniel De Graaf [[email protected]]
Sent: 08 September 2010 18:10
To: Jeremy Fitzhardinge
Cc: xen-devel; Jun Zhu (Intern)
Subject: [PATCH] xenbus: avoid zero returns from read()

It is possible to get a zero return from read() in instances where the queue is not empty but has no elements with data to deliver to the user. Since a zero return from read is an error indicator, resume waiting or return -EAGAIN (for a nonblocking fd) in this case.

---

drivers/xen/xenfs/xenbus.c +3 -0
index 88c87c9..0ddef43 100644
@@ -121,6 +121,7 @@ static ssize_t xenbus_file_read(struct file *filp,
        int ret;
 
        mutex_lock(&u->reply_mutex);
+again:
        while (list_empty(&u->read_buffers)) {
                mutex_unlock(&u->reply_mutex);
                if (filp->f_flags & O_NONBLOCK)
@@ -159,6 +160,8 @@ static ssize_t xenbus_file_read(struct file *filp,
                                        struct read_buffer, list);
                }
        }
+       if (i == 0)
+               goto again;
 
 out:
        mutex_unlock(&u->reply_mutex);
#2 lore raw
Quoted 1 line · Jun Zhu (Intern)

Is it related to the following patch? The following patch fixes the problem of queue deletion.

Yes, this would have caused a zero return due to an empty item being left in the queue. It's likely my patch is not needed with this one applied; we are already careful to avoid adding zero-length elements to the queue, which was my original idea for the cause.

Quoted 52 lines
drivers/xen/xenfs/xenbus.c +1 -1
index 64b3be4..763e90d 100644
@@ -143,7 +143,7 @@ static ssize_t xenbus_file_read(struct file *filp,
                i += sz - ret;
                rb->cons += sz - ret;
 
-               if (ret != sz) {
+               if (ret != 0) {
                        if (i == 0)
                                i = -EFAULT;
                        goto out;

Jun Zhu
Citrix Systems UK
________________________________________
From: Daniel De Graaf [[email protected]]
Sent: 08 September 2010 18:10
To: Jeremy Fitzhardinge
Cc: xen-devel; Jun Zhu (Intern)
Subject: [PATCH] xenbus: avoid zero returns from read()

It is possible to get a zero return from read() in instances where the queue is not empty but has no elements with data to deliver to the user. Since a zero return from read is an error indicator, resume waiting or return -EAGAIN (for a nonblocking fd) in this case.

---

drivers/xen/xenfs/xenbus.c +3 -0
index 88c87c9..0ddef43 100644
@@ -121,6 +121,7 @@ static ssize_t xenbus_file_read(struct file *filp,
        int ret;
 
        mutex_lock(&u->reply_mutex);
+again:
        while (list_empty(&u->read_buffers)) {
                mutex_unlock(&u->reply_mutex);
                if (filp->f_flags & O_NONBLOCK)
@@ -159,6 +160,8 @@ static ssize_t xenbus_file_read(struct file *filp,
                                        struct read_buffer, list);
                }
        }
+       if (i == 0)
+               goto again;
 
 out:
        mutex_unlock(&u->reply_mutex);
Signature · 2 lines
Daniel De Graaf
National Security Agency
#3 lore raw
Quoted 6 lines · Daniel De Graaf
Quoted 1 line · Jun Zhu (Intern)

Is it related to the following patch? The following patch fixes the problem of queue deletion.

Yes, this would have caused a zero return due to an empty item being left in the queue. It's likely my patch is not needed with this one applied; we are already careful to avoid adding zero-length elements to the queue, which was my original idea for the cause.

No, that's a separate issue. This patch fixes the case where the usermode buffer is near a page boundary so the copy to usermode is truncated.

Anyway, I've applied both.

J

Quoted 53 lines
Quoted 52 lines
drivers/xen/xenfs/xenbus.c +1 -1
index 64b3be4..763e90d 100644
@@ -143,7 +143,7 @@ static ssize_t xenbus_file_read(struct file *filp,
                i += sz - ret;
                rb->cons += sz - ret;
 
-               if (ret != sz) {
+               if (ret != 0) {
                        if (i == 0)
                                i = -EFAULT;
                        goto out;

Jun Zhu
Citrix Systems UK
________________________________________
From: Daniel De Graaf [[email protected]]
Sent: 08 September 2010 18:10
To: Jeremy Fitzhardinge
Cc: xen-devel; Jun Zhu (Intern)
Subject: [PATCH] xenbus: avoid zero returns from read()

It is possible to get a zero return from read() in instances where the queue is not empty but has no elements with data to deliver to the user. Since a zero return from read is an error indicator, resume waiting or return -EAGAIN (for a nonblocking fd) in this case.

---

drivers/xen/xenfs/xenbus.c +3 -0
index 88c87c9..0ddef43 100644
@@ -121,6 +121,7 @@ static ssize_t xenbus_file_read(struct file *filp,
        int ret;
 
        mutex_lock(&u->reply_mutex);
+again:
        while (list_empty(&u->read_buffers)) {
                mutex_unlock(&u->reply_mutex);
                if (filp->f_flags & O_NONBLOCK)
@@ -159,6 +160,8 @@ static ssize_t xenbus_file_read(struct file *filp,
                                        struct read_buffer, list);
                }
        }
+       if (i == 0)
+               goto again;
 
 out:
        mutex_unlock(&u->reply_mutex);
#4 lore raw
Quoted 8 lines · Jeremy Fitzhardinge
Quoted 6 lines · Daniel De Graaf
Quoted 1 line · Jun Zhu (Intern)

Is it related to the following patch? The following patch fixes the problem of queue deletion.

Yes, this would have caused a zero return due to an empty item being left in the queue. It's likely my patch is not needed with this one applied; we are already careful to avoid adding zero-length elements to the queue, which was my original idea for the cause.

Quoted 7 lines

No, that's a separate issue. This patch fixes the case where the usermode buffer is near a page boundary so the copy to usermode is truncated.

Anyway, I've applied both.

J

One or both of these patches are needed in the 2.6.37 kernel; I have observed a zero return from a read() on xenbus on 2.6.37-rc8 that is fixed by this patch.

Quoted 59 lines
Quoted 50 lines
Quoted 50 lines
drivers/xen/xenfs/xenbus.c +1 -1
index 64b3be4..763e90d 100644
@@ -143,7 +143,7 @@ static ssize_t xenbus_file_read(struct file *filp,
                i += sz - ret;
                rb->cons += sz - ret;
 
-               if (ret != sz) {
+               if (ret != 0) {
                        if (i == 0)
                                i = -EFAULT;
                        goto out;

Jun Zhu
Citrix Systems UK
________________________________________
From: Daniel De Graaf [[email protected]]
Sent: 08 September 2010 18:10
To: Jeremy Fitzhardinge
Cc: xen-devel; Jun Zhu (Intern)
Subject: [PATCH] xenbus: avoid zero returns from read()

It is possible to get a zero return from read() in instances where the queue is not empty but has no elements with data to deliver to the user. Since a zero return from read is an error indicator, resume waiting or return -EAGAIN (for a nonblocking fd) in this case.

---

drivers/xen/xenfs/xenbus.c +3 -0
index 88c87c9..0ddef43 100644
@@ -121,6 +121,7 @@ static ssize_t xenbus_file_read(struct file *filp,
        int ret;
 
        mutex_lock(&u->reply_mutex);
+again:
        while (list_empty(&u->read_buffers)) {
                mutex_unlock(&u->reply_mutex);
                if (filp->f_flags & O_NONBLOCK)
@@ -159,6 +160,8 @@ static ssize_t xenbus_file_read(struct file *filp,
                                        struct read_buffer, list);
                }
        }
+       if (i == 0)
+               goto again;
 
Quoted 3 lines

drivers/xen/xenfs/xenbus.c static ssize_t xenbus_file_read(struct file *filp, · line 167

Quoted 2 lines
drivers/xen/xenfs/xenbus.c +0 -0
 out:
        mutex_unlock(&u->reply_mutex);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

Signature · 2 lines
Daniel De Graaf
National Security Agency