Skip to contentSkip to conversation

xen/pvcalls: return -EINTR from recvmsg when the wait is interrupted

PATCH New Suyuan Wang 1 participant 1 message Nov 18 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/pvcalls: return -EINTR from recvmsg when the wait is interrupted

From: Suyuan Wang <[email protected]>

pvcalls_front_recvmsg() waits for data in a loop and ignores what wait_event_interruptible() returns:

    while (!(flags & MSG_DONTWAIT) && !pvcalls_front_read_todo(map)) {
            wait_event_interruptible(map->active.inflight_conn_req,
                                     pvcalls_front_read_todo(map));
    }

Once a signal is pending, wait_event_interruptible() returns -ERESTARTSYS right away without sleeping, and the loop calls it again. The caller then spins with map->active.in_mutex held until data arrives or the backend reports an error. The signal can't be handled while it spins, so the task can't be killed either, and other readers of the socket block on in_mutex.

Return -EINTR when the wait is interrupted, as pvcalls_front_accept() already does for its interruptible waits. Jump to the end of the function so that in_mutex is released and pvcalls_exit_sock() is called.

Found with a static analysis tool and checked by hand.

Fixes ae0d04052e07 ("xen/pvcalls: implement recvmsg")

---
There are no in-tree callers of pvcalls_front_recvmsg(), so this only matters to code built on the exported frontend API. That's why I left out Cc: stable.

Compile tested only (x86_64 with CONFIG_XEN_PVCALLS_FRONTEND=y, W=1 and sparse show no new warnings). I haven't run it in a Xen guest.

---
 drivers/xen/pvcalls-front.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/xen/pvcalls-front.c +6 -2
index 3e7aa807c317..5d6c8ba66849 100644
@@ -693,8 +693,11 @@ int pvcalls_front_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
 		len = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
 
 	while (!(flags & MSG_DONTWAIT) && !pvcalls_front_read_todo(map)) {
-		wait_event_interruptible(map->active.inflight_conn_req,
-					 pvcalls_front_read_todo(map));
+		if (wait_event_interruptible(map->active.inflight_conn_req,
+					     pvcalls_front_read_todo(map))) {
+			ret = -EINTR;
+			goto out;
+		}
 	}
 	ret = __read_ring(map->active.ring, &map->active.data,
 			  &msg->msg_iter, len, flags);
@@ -706,6 +709,7 @@ int pvcalls_front_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
 	if (ret == -ENOTCONN)
 		ret = 0;
 
+out:
 	mutex_unlock(&map->active.in_mutex);
 	pvcalls_exit_sock(sock);
 	return ret;