From patchwork Tue Feb 25 15:14:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 1244354 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=O2jJfMr9; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48RjNQ4gL1z9sPk for ; Wed, 26 Feb 2020 02:22:30 +1100 (AEDT) Received: from localhost ([::1]:58704 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6c2W-0006mv-HT for incoming@patchwork.ozlabs.org; Tue, 25 Feb 2020 10:22:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55936) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6buz-0001ux-Kn for qemu-devel@nongnu.org; Tue, 25 Feb 2020 10:14:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6buy-0001hV-D9 for qemu-devel@nongnu.org; Tue, 25 Feb 2020 10:14:41 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:29634 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j6buy-0001fh-83 for qemu-devel@nongnu.org; Tue, 25 Feb 2020 10:14:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582643678; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PaVZ83rgHHJzvD04uhNEPYZywTa4yNbe9G1r2BQHUDs=; b=O2jJfMr9NpoOQ+dCOsKPvAPu/5wYTU2+THzKlWBr6dxxbYR0l2zdCZW2ET7eoQWnZPO1yk 8oE9FXVYQE3hf6jwe2GXhp8sklJnV6QSUJoSXIdq1Pn4iMIC+A+lLHmW//9hZ6utIvwZGE SNT0Z4PdZTLh9dPL1Qq1DQ8qOnnA+Pw= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-170-RsKhekESMMulHudztwt3GQ-1; Tue, 25 Feb 2020 10:14:36 -0500 X-MC-Unique: RsKhekESMMulHudztwt3GQ-1 Received: by mail-qt1-f200.google.com with SMTP id d9so15201930qtq.13 for ; Tue, 25 Feb 2020 07:14:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=BEIFZp6ZV6EMxNZvhYr+RG8aqKLzp8+FfOWRj9nMRTw=; b=X5fo05QRanVIPwD9y+FbRjy27dccPkBkFMkkR6ByODXWRarw3i0Bc5yK4Pd6IiW152 xgZrhS0cNomNrnnBK0nBmlks0zGJ0cHhomz2hDaoARjG0EehMv4BbCyAnCG5dpvRVIy+ xLuE+9yVGyIQwLm8LxjmcVrr38JvL5zcUS/FAlpREMHUuKS7mwr32hteILCrsCJxcPrA b4rud904XXK0HFX7HKYBDqvLDg8gmQE+tpxbRBSzeC8dLUGuaqPZL767r3yyxasDQXBg U0rk699TMpuBCuFPJPd3DlYHS8W8Q4lNEcAwwToBlSwh0/DG6eJdsYElp9aL+MA8g2R8 46JA== X-Gm-Message-State: APjAAAWxJVqbJXhIuXV40R2x4NmKlyZ6Lq94DksTjF6OwKnvaK3a9cL8 zBqGBOzSxqDD3FEhrqdc5F2jA2hatXUfp/E2HVeDDB4n4Pw//IhF1LgHCZirWCLlaONKA794+Nw mlh3Do5YR0qkYpn0= X-Received: by 2002:aed:37a4:: with SMTP id j33mr3919252qtb.363.1582643675578; Tue, 25 Feb 2020 07:14:35 -0800 (PST) X-Google-Smtp-Source: APXvYqzTJ0QI3azMUZ1bYT7H35iT1Lt8umIsgba9SBNyIzNGt9m7DpqRQuBfbixJfZZ9V5ppy4Olcg== X-Received: by 2002:aed:37a4:: with SMTP id j33mr3919222qtb.363.1582643675326; Tue, 25 Feb 2020 07:14:35 -0800 (PST) Received: from redhat.com (bzq-79-178-2-214.red.bezeqint.net. [79.178.2.214]) by smtp.gmail.com with ESMTPSA id d1sm7436695qkj.29.2020.02.25.07.14.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2020 07:14:34 -0800 (PST) Date: Tue, 25 Feb 2020 10:14:30 -0500 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Subject: [PULL 25/32] libvhost-user: handle NOFD flag in call/kick/err better Message-ID: <20200225151210.647797-26-mst@redhat.com> References: <20200225151210.647797-1-mst@redhat.com> MIME-Version: 1.0 In-Reply-To: <20200225151210.647797-1-mst@redhat.com> X-Mailer: git-send-email 2.24.1.751.gd10ce2899c X-Mutt-Fcc: =sent X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Stefan Hajnoczi , Gerd Hoffmann , Johannes Berg , =?utf-8?q?Marc-Andr=C3=A9?= Lureau Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Johannes Berg The code here is odd, for example will it print out invalid file descriptor numbers that were never sent in the message. Clean that up a bit so it's actually possible to implement a device that uses polling. Signed-off-by: Johannes Berg Message-Id: <20200123081708.7817-5-johannes@sipsolutions.net> Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- contrib/libvhost-user/libvhost-user.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/contrib/libvhost-user/libvhost-user.c b/contrib/libvhost-user/libvhost-user.c index 533d55d82a..3abc9689e5 100644 --- a/contrib/libvhost-user/libvhost-user.c +++ b/contrib/libvhost-user/libvhost-user.c @@ -948,6 +948,7 @@ static bool vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg) { int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK; + bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK; if (index >= dev->max_queues) { vmsg_close_fds(vmsg); @@ -955,8 +956,12 @@ vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg) return false; } - if (vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK || - vmsg->fd_num != 1) { + if (nofd) { + vmsg_close_fds(vmsg); + return true; + } + + if (vmsg->fd_num != 1) { vmsg_close_fds(vmsg); vu_panic(dev, "Invalid fds in request: %d", vmsg->request); return false; @@ -1053,6 +1058,7 @@ static bool vu_set_vring_kick_exec(VuDev *dev, VhostUserMsg *vmsg) { int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK; + bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK; DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64); @@ -1066,8 +1072,8 @@ vu_set_vring_kick_exec(VuDev *dev, VhostUserMsg *vmsg) dev->vq[index].kick_fd = -1; } - dev->vq[index].kick_fd = vmsg->fds[0]; - DPRINT("Got kick_fd: %d for vq: %d\n", vmsg->fds[0], index); + dev->vq[index].kick_fd = nofd ? -1 : vmsg->fds[0]; + DPRINT("Got kick_fd: %d for vq: %d\n", dev->vq[index].kick_fd, index); dev->vq[index].started = true; if (dev->iface->queue_set_started) { @@ -1147,6 +1153,7 @@ static bool vu_set_vring_call_exec(VuDev *dev, VhostUserMsg *vmsg) { int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK; + bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK; DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64); @@ -1159,14 +1166,14 @@ vu_set_vring_call_exec(VuDev *dev, VhostUserMsg *vmsg) dev->vq[index].call_fd = -1; } - dev->vq[index].call_fd = vmsg->fds[0]; + dev->vq[index].call_fd = nofd ? -1 : vmsg->fds[0]; /* in case of I/O hang after reconnecting */ - if (eventfd_write(vmsg->fds[0], 1)) { + if (dev->vq[index].call_fd != -1 && eventfd_write(vmsg->fds[0], 1)) { return -1; } - DPRINT("Got call_fd: %d for vq: %d\n", vmsg->fds[0], index); + DPRINT("Got call_fd: %d for vq: %d\n", dev->vq[index].call_fd, index); return false; } @@ -1175,6 +1182,7 @@ static bool vu_set_vring_err_exec(VuDev *dev, VhostUserMsg *vmsg) { int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK; + bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK; DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64); @@ -1187,7 +1195,7 @@ vu_set_vring_err_exec(VuDev *dev, VhostUserMsg *vmsg) dev->vq[index].err_fd = -1; } - dev->vq[index].err_fd = vmsg->fds[0]; + dev->vq[index].err_fd = nofd ? -1 : vmsg->fds[0]; return false; }