From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 084B6434E33 for ; Tue, 21 Jul 2026 07:37:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784619426; cv=none; b=Ju+hT7G6bikggb6jpI1YdxiJcyns8zRmw72hJqVYA0ozkeaQZvcNt9zO615wV6ypJ7IEZkwL3/A0+in36k5LnqWLmlFxyuHxysqf0kPnZuIPEhZjwxycrFaAvPc9fTjziQt9L81pTh0GEv9uqeV7S8si+xf5SvK59qw7+x6Shvc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784619426; c=relaxed/simple; bh=PBJgJTVktA6dk4pRBadGmyyYxpIPFy/6kLS3RcLYINk=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=C8JhihDO1kWLzUVKtjYda6NZHKiTiRPIUD9u6k7XdeFQCxEp63tq0jDbxT+dLv8z/zgqeNAZyVcUp/R0e6xgVKfVSMvHz8cg9hBm1WyWmJbbHSjQUly6agZg5btYA/kYdXKwEvSQGaD2bf4kaIgb9xHYNfnUAj+cxmYhKRnFpDU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OwTAeriS; arc=none smtp.client-ip=209.85.216.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OwTAeriS" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-385ea3ce80dso11113403a91.2 for ; Tue, 21 Jul 2026 00:37:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784619424; x=1785224224; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=35d5h6tDV+eeR0qT67WDhTn7rqN/E2touheot1irCJc=; b=OwTAeriSR0SXhCuEDSNWf5ABehzQEn9lCc8GVy43GCe6hFM2R9V2ncGsrN++bWpUwO SWNCnlDguSyQZSuGqP7xi0v0l6N1HK2lqZysA75VAVBT/tdfX0BfGuC7z/xuylt6nCYL 0302ht3PRiruJRMXJfgQBOISOY9o0Hlhn/KTMQKgs1mExLYR9/fwSd2/1GUYXeR2Z34B MuWE47wkKRAL7plDjFNwvbw5zCSVhldkCsazyzYl6VC+mawQfBL1dDqoASmHnVtKE/Sl OVH9gW+zHOJexdpJHq/MTBSGkLdYg4bOvjva/q9aMve9jZ3xRjJtMUqttCc9pF6BkBcr vf5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784619424; x=1785224224; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=35d5h6tDV+eeR0qT67WDhTn7rqN/E2touheot1irCJc=; b=rUQ0oLJmF+p5x5p3K6Bh7ZZcTwc9HSlWFw1px6ACEYYgMB9zl7NungL0LMwmzgBc7L 5qlE6RQihFHeRUs5UF3PFJrDCa4WV29rkXsRxGFBG4ZEisDuOFzBO1/MWiVGDjVuWeGI C4D7eAAxbM7q4TrUgDP8Z/BCcorXaOQ3/BLV+dNkVtpO5++xu86QaMauAV0YvjOHOX3z K0lM+iiBrwqwUsqFxx6DufbVRmkJ9pM1qywAxVVuM22aEEYTBnc0Wc02zDltAD3FRNjJ WXzcuHw2XPGbLT1cyI+svfO+2l0MG6b/6Z6ZUhwif1R6M+fGgr6BJ6CbS7JArNJd0ETB GKGg== X-Forwarded-Encrypted: i=1; AHgh+RqVtbMOJ7+KHX+fNSPX97Y88ohXSmJLXhNiOHeHdjg8XqsAP7plblFKnKEiglHs5aGG3gIQqXo=@vger.kernel.org X-Gm-Message-State: AOJu0YxwFhQ9/5k0CErirC7/x/NROTw6Z5kI3xSCnkhTjiR6oEny1a74 L/RXqwgZTatVJ/bdOttUisJJnQj1W0HFiyu48gl8dzHezejWGySWua7S X-Gm-Gg: AR+sD10ulSfMiinWpD1Vo2t6aJ8viVtW7tnmwYQJ0LsNKlOTuq9z3ZiPVRbn7w0TE9U xIzZ8Oei8hrrcWwoE0P3hhnsRIfiqFJH6gx1ar8abUCmJSbFzaibYayvrdgcAZHqq7GrwYdNPx1 RqrMuWTpHBpCIO5TQxFskpYbDJ7+NUnWIa6gqNYFMuxaUDfcFwI1938dRhgb/Bmx/d+UOQ4tgEI QSSJtm2FJ9Mt1P/HZBg8URkkAbjuRfUiNpIh/5BcEzrWx+YbnNgniL8vebod+QzUrZyhVI/ZOjt RexSeIupbeKIr5yiDv78GtA2JszQobGz6Fk3/8ZhBhryQpMANSGhaiILe8YSYqdjF9p5nbLjGdL TVaOpaLwbFbMujm40umyksTqW+Cpb7mqNC+D43IEh9kioKhSi3aJxJpOsjLBtxMgh4z/jzg== X-Received: by 2002:a17:90a:c885:b0:380:83fc:4315 with SMTP id 98e67ed59e1d1-38e4b538938mr18875650a91.21.1784619423827; Tue, 21 Jul 2026 00:37:03 -0700 (PDT) Received: from jia ([188.253.12.32]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-38e92169cebsm1070482a91.11.2026.07.21.00.37.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jul 2026 00:37:03 -0700 (PDT) From: Jia Jia To: "Michael S . Tsirkin" , Jason Wang , Mike Christie Cc: Paolo Bonzini , Stefan Hajnoci , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , virtualization@lists.linux.dev, kvm@vger.kernel.org, netdev@vger.kernel.org, Jia Jia Subject: [PATCH] vhost-scsi: flush backend after device ioctls Date: Tue, 21 Jul 2026 15:36:39 +0800 Message-Id: <20260721073639.1532488-1-physicalmtea@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit vhost-scsi translates guest response descriptors into userspace iovecs at command submission time and later completes those commands asynchronously through target-core. Device-wide control operations such as VHOST_SET_MEM_TABLE replace the memory table under the device and virtqueue mutexes, but historically returned without waiting for outstanding SCSI commands that still hold the pre-update response iovecs. After such a replacement, completion may write virtio_scsi_cmd_resp through the old host virtual addresses. If the owner has already remapped those addresses, the write lands on the wrong userspace object. The kernel tree has carried a TODO for this since the 2012 split of vhost_dev_ioctl() and vhost_vring_ioctl(): /* TODO: flush backend after dev ioctl. */ A userspace test kept a READ(10) pending, replaced the memory table so the response GPA mapped to a new HVA, remapped the old response address as a victim page, and then let the command complete. The completion wrote the victim page (victim_changed=yes) and left the replacement page unchanged; a later TUR updated the new mapping instead. So the pending command retained the pre-update response address across VHOST_SET_MEM_TABLE. That same 2012 change deliberately avoided a second backend flush on the vring-ioctl path: vring updates already flush where appropriate, and an extra heavy flush would hurt when kick or call fds are reconfigured on the data path. This fix does not reintroduce that. The default branch still routes unknown commands through vhost_dev_ioctl() first; only a non-ENOIOCTLCMD result flushes. Vring ops such as SET_VRING_KICK/CALL, num, addr, and base return -ENOIOCTLCMD there and fall through to vhost_vring_ioctl() without this backend flush. What vhost_dev_ioctl() actually handles on this path is small: VHOST_SET_OWNER, VHOST_SET_MEM_TABLE, VHOST_SET_LOG_BASE, VHOST_SET_LOG_FD, and the optional fork-owner ioctls when enabled. Flushing after those is fine: they are rare device-wide control ops, and SET_OWNER normally runs before any inflight SCSI work. Call vhost_scsi_flush() so pre-update worker work and target-core inflight commands finish before the ioctl returns. As with the existing net pattern, any non-ENOIOCTLCMD result flushes, including failures that may have applied a partial update such as VHOST_SET_LOG_BASE. I later noticed vhost-net and vhost-vsock already use the same device versus vring split. This is a control-plane barrier only. Ordinary submission, completion, kick, and call paths are unchanged. The owner is expected to keep pre-update mappings valid until the device ioctl returns. Completion copies the response and signals from the vhost worker without needing further userspace progress, so waiting in this ioctl does not leave the owner process stuck on itself. Signed-off-by: Jia Jia --- drivers/vhost/scsi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c index 9a1253b9d8c5..c3e8f1a0b2d4 100644 --- a/drivers/vhost/scsi.c +++ b/drivers/vhost/scsi.c @@ -2424,10 +2424,11 @@ vhost_scsi_ioctl(struct file *f, unsigned int ioctl, unsigned long arg) default: mutex_lock(&vs->dev.mutex); r = vhost_dev_ioctl(&vs->dev, ioctl, argp); - /* TODO: flush backend after dev ioctl. */ if (r == -ENOIOCTLCMD) r = vhost_vring_ioctl(&vs->dev, ioctl, argp); + else + vhost_scsi_flush(vs); mutex_unlock(&vs->dev.mutex); return r; } } -- 2.43.0