From patchwork Fri Feb 7 18:41:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongli Zhang X-Patchwork-Id: 13965582 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21DEC1A3144; Fri, 7 Feb 2025 18:43:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.165.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953783; cv=none; b=RQyIB4jRuw45EK1IuNB3tpXM68hN9Q5ZKtAvIW7WFJPlOmlYPqWvHoUCyHXXz+q2K6gtzrLJ6SCUke52AsfESNyeKSUemfcu78EM5L3veM96MohMRPDBYkxBo2AylWR+tqt/93bKTg2HMHPEdL2hOB8qZJ5WcCalNim4K2SasTQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953783; c=relaxed/simple; bh=GjBNQ6+NVFKyJGJPRf5FY78SvbrtMuBCufejpM0iu5Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hD0BiJ23tBniTcYjP8fXTHFEA9ummvXe2PdaMFDv9nmWJuBnMDAdQfz5ORfJDQfHvyQJlPH15y2nkHXImdgn1p8i/IeTT822JEK6GFQWSMtjJvUEZuaP5nnTqGAuqIF0DyvIIUCJF3WY5RU6IhGTa8LmgLpu0oeYkIay02VE8IE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=bcolcxjv; arc=none smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="bcolcxjv" Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 517Hg6nC000480; Fri, 7 Feb 2025 18:42:56 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=Q11OU aRwLsU4W0d17uY17NUHcyRRAjGZ3xTHFdH0R20=; b=bcolcxjv/jFQqbNjL8vAb pOb8SY4FgxATIvAPedMXkMy0sIDPUZ3vo5vyNOcJ3VDjzykQhtSnp3IWOHGOa96/ sI8KU0oSGzwNVqFFKvWF386RidrU+NzlGzW0MY03qsdQ+1qv/QJ0DU7d90u+aTOt bbnHvgbrxZ3+KyQFgv5RVthvH5uP1T6cE4Su0jmL+VtP5bH4OAbqhjXr40x67DnU pdrKnnd+bdB+AvHrIYTCAvjzzNX4uLDD9QmtwFmYi+lB7mwgFe0CsNTOX2t+OF77 +LSfxmD+/Zluadu3UUqWZPwPl4r8TjQspt+4gzs9zNeQLSh2KLMzUx8AJ1KYkCzz Q== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 44mqk8bbgy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:42:56 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 517H9omS022526; Fri, 7 Feb 2025 18:42:55 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 44j8ec8677-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:42:55 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 517Idx9L037660; Fri, 7 Feb 2025 18:42:55 GMT Received: from localhost.localdomain (ca-dev80.us.oracle.com [10.211.9.80]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 44j8ec8665-2; Fri, 07 Feb 2025 18:42:55 +0000 From: Dongli Zhang To: virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org Cc: mst@redhat.com, jasowang@redhat.com, eperezma@redhat.com, michael.christie@oracle.com, pbonzini@redhat.com, stefanha@redhat.com, joao.m.martins@oracle.com, joe.jin@oracle.com, si-wei.liu@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH 1/9] vhost: modify vhost_log_write() for broader users Date: Fri, 7 Feb 2025 10:41:45 -0800 Message-ID: <20250207184212.20831-2-dongli.zhang@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250207184212.20831-1-dongli.zhang@oracle.com> References: <20250207184212.20831-1-dongli.zhang@oracle.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-02-07_08,2025-02-07_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=957 phishscore=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2501170000 definitions=main-2502070139 X-Proofpoint-GUID: lXJ6qRw6Q2un2jBGHSZcBIJz_aFmPpgq X-Proofpoint-ORIG-GUID: lXJ6qRw6Q2un2jBGHSZcBIJz_aFmPpgq Currently, the only user of vhost_log_write() is vhost-net. The 'len' argument prevents logging of pages that are not tainted by the RX path. Adjustments are needed since more drivers (i.e. vhost-scsi) begin using vhost_log_write(). So far vhost-net RX path may only partially use pages shared by the last vring descriptor. Unlike vhost-net, vhost-scsi always logs all pages shared via vring descriptors. To accommodate this, a new argument 'partial' is introduced. This argument works alongside 'len' to indicate whether the driver should log all pages of a vring descriptor, or only pages that are tainted by the driver. In addition, removes BUG(). Suggested-by: Joao Martins Signed-off-by: Dongli Zhang --- drivers/vhost/net.c | 2 +- drivers/vhost/vhost.c | 28 +++++++++++++++++----------- drivers/vhost/vhost.h | 2 +- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c index b9b9e9d40951..0e5d82bfde76 100644 --- a/drivers/vhost/net.c +++ b/drivers/vhost/net.c @@ -1219,7 +1219,7 @@ static void handle_rx(struct vhost_net *net) if (nvq->done_idx > VHOST_NET_BATCH) vhost_net_signal_used(nvq); if (unlikely(vq_log)) - vhost_log_write(vq, vq_log, log, vhost_len, + vhost_log_write(vq, vq_log, log, vhost_len, true, vq->iov, in); total_len += vhost_len; } while (likely(!vhost_exceeds_weight(vq, ++recv_pkts, total_len))); diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 9ac25d08f473..db3b30aba940 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2304,8 +2304,14 @@ static int log_used(struct vhost_virtqueue *vq, u64 used_offset, u64 len) return 0; } -int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, - unsigned int log_num, u64 len, struct iovec *iov, int count) +/* + * 'len' is used only when 'partial' is true, to indicate whether the + * entire length of each descriptor is logged. + */ +int vhost_log_write(struct vhost_virtqueue *vq, + struct vhost_log *log, unsigned int log_num, + u64 len, bool partial, + struct iovec *iov, int count) { int i, r; @@ -2323,19 +2329,19 @@ int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, } for (i = 0; i < log_num; ++i) { - u64 l = min(log[i].len, len); + u64 l = partial ? min(log[i].len, len) : log[i].len; + r = log_write(vq->log_base, log[i].addr, l); if (r < 0) return r; - len -= l; - if (!len) { - if (vq->log_ctx) - eventfd_signal(vq->log_ctx); - return 0; - } + + if (partial) + len -= l; } - /* Length written exceeds what we have stored. This is a bug. */ - BUG(); + + if (vq->log_ctx) + eventfd_signal(vq->log_ctx); + return 0; } EXPORT_SYMBOL_GPL(vhost_log_write); diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index bb75a292d50c..5de5941988fe 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -224,7 +224,7 @@ bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *); bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *); int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, - unsigned int log_num, u64 len, + unsigned int log_num, u64 len, bool partial, struct iovec *iov, int count); int vq_meta_prefetch(struct vhost_virtqueue *vq); From patchwork Fri Feb 7 18:41:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongli Zhang X-Patchwork-Id: 13965583 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21E6A1A3145; Fri, 7 Feb 2025 18:43:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.165.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953783; cv=none; b=bmf79ewxmhgERcyILOxeLzwVYr/C2OQokaqRT19DYFrRrWGJJlNsXYuf41yRxEZifZPSR8IQ39ZKW3HawabDO2IeUqmoR2fFEQsskNQRIMa7jRIYc/QTJAu4BiBLHa/N9TnfJPMPI1PMMS2yPtwoy/obIifW2xfgjPnNQE3j5zc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953783; c=relaxed/simple; bh=KZpYGCgP90T/gN1JAyUvL6ue46fXpr3ZBw6UD+zQaSc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iA3TWUe9bvFD5QytV+aeWTzvF00agpiRGUH7WLGS/lU2xM6qXX3f6NDRaRPg4A4EB32HPdvsdwLext1pISuHmDGUgzLo/F9JoJAtBlrr/HJrqzGfDnBLKttSuyiXL3cjIXDd8SRftOhv9ARn1NmXHZsqwN/MfukBz5JBW8FHd+g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=V8ppnauY; arc=none smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="V8ppnauY" Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 517Hg6xv000482; Fri, 7 Feb 2025 18:42:57 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=Ud1cq X2B/Bk8a2z8lXNW+8CRC+VrCZiQVXNoaj7J2aQ=; b=V8ppnauYKhDf/ef/8Meqx Ca3X4jihY09ndkSjy652lQ49VBDseL9SfSPz8GjlG8STpn8lKuBP4JjjuZPjZkye JBjoE5cF2ancexNRK3Xd4FNb81/vBUq4SJYYtdchgHOsQi9fIOZdPeBOgsy1eNMW tNpBsEXmRU3UHGkw61SMBP7GVOiUKtDdL1SrPfF3DE6k5BOq4lAUoRcTWaoz//UR 5ND2+smqKaHCMr5bKHOmd0trJumsifZRX7f5NJV5+oaIk++X/lg381wKaUp+cTjk PY8VMHAA+8Rm7d64c4JeGOSBr43ab3JO/aHtmCajdBCyuPo1zflzt1lJbkqSkke0 A== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 44mqk8bbh0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:42:57 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 517HiGLA022576; Fri, 7 Feb 2025 18:42:56 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 44j8ec867n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:42:56 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 517Idx9N037660; Fri, 7 Feb 2025 18:42:56 GMT Received: from localhost.localdomain (ca-dev80.us.oracle.com [10.211.9.80]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 44j8ec8665-3; Fri, 07 Feb 2025 18:42:56 +0000 From: Dongli Zhang To: virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org Cc: mst@redhat.com, jasowang@redhat.com, eperezma@redhat.com, michael.christie@oracle.com, pbonzini@redhat.com, stefanha@redhat.com, joao.m.martins@oracle.com, joe.jin@oracle.com, si-wei.liu@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH 2/9] vhost-scsi: adjust vhost_scsi_get_desc() to log vring descriptors Date: Fri, 7 Feb 2025 10:41:46 -0800 Message-ID: <20250207184212.20831-3-dongli.zhang@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250207184212.20831-1-dongli.zhang@oracle.com> References: <20250207184212.20831-1-dongli.zhang@oracle.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-02-07_08,2025-02-07_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 phishscore=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2501170000 definitions=main-2502070139 X-Proofpoint-GUID: LqyZ_sjfs2KJG3yuPs4Dcj8sh6sNCnPD X-Proofpoint-ORIG-GUID: LqyZ_sjfs2KJG3yuPs4Dcj8sh6sNCnPD Adjust vhost_scsi_get_desc() to facilitate logging of vring descriptors. Add new arguments to allow passing the log buffer and length to vhost_get_vq_desc(). In addition, reset 'log_num' since vhost_get_vq_desc() may reset it only after certain condition checks. Suggested-by: Joao Martins Signed-off-by: Dongli Zhang --- drivers/vhost/scsi.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c index 718fa4e0b31e..ee2310555740 100644 --- a/drivers/vhost/scsi.c +++ b/drivers/vhost/scsi.c @@ -950,13 +950,17 @@ vhost_scsi_send_bad_target(struct vhost_scsi *vs, static int vhost_scsi_get_desc(struct vhost_scsi *vs, struct vhost_virtqueue *vq, - struct vhost_scsi_ctx *vc) + struct vhost_scsi_ctx *vc, + struct vhost_log *log, unsigned int *log_num) { int ret = -ENXIO; + if (likely(log_num)) + *log_num = 0; + vc->head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov), &vc->out, &vc->in, - NULL, NULL); + log, log_num); pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n", vc->head, vc->out, vc->in); @@ -1086,7 +1090,7 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) vhost_disable_notify(&vs->dev, vq); do { - ret = vhost_scsi_get_desc(vs, vq, &vc); + ret = vhost_scsi_get_desc(vs, vq, &vc, NULL, NULL); if (ret) goto err; @@ -1411,7 +1415,7 @@ vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) vhost_disable_notify(&vs->dev, vq); do { - ret = vhost_scsi_get_desc(vs, vq, &vc); + ret = vhost_scsi_get_desc(vs, vq, &vc, NULL, NULL); if (ret) goto err; From patchwork Fri Feb 7 18:41:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongli Zhang X-Patchwork-Id: 13965587 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0BEAB1A3161; Fri, 7 Feb 2025 18:43:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.177.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953786; cv=none; b=aYnMXb0PYkKKQIlYEZZvh82FPk25tWhZUnfMCC7G8/SzFaj2QpUDspj3Or1g9y3az3BtJFXjjDllsOtJ9k2FDAetXukpjz8y5+0pNLU+ieuG/Us+XPictKg5uYQP+XepbKWgVldDg1dxv8HUFZTGE4KTGfN5dfb0dAo5061KP7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953786; c=relaxed/simple; bh=9KcNunuYxOHLcipbsIYeWGmwHZWT2GJbR8DrZq+P0yE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BFvauGDgzf8dCNMWWJtP4ckOdAyCFQi8zfnk144pzMhRWvyr4o5vHUnSDkVjnffbYbbGY0DtxI5+WJKhPvGOJx2+PNXQlukwV6Ritnj4KVFSm9C2doXyp1cvaYtMhDXJoAIT5+pA7nHc71TKs153CvM7+/+BCp2IsQJyxZMSs7A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=SypMYZDU; arc=none smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="SypMYZDU" Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 517HfrVc009269; Fri, 7 Feb 2025 18:42:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=c5XwY UKc7W9La6YjGqr4UmbZPDEugwITEt8f/5aGINU=; b=SypMYZDU1G4qWTcGtsxFn Oom9xbEoDQVbaGATU5AV4YtiFasrnC0n0QTKurJymPnO6SuJy20FJjuV2ErxoW2O YKAL9dV+z4UYz2KP36QnoRLJjgE+6BGvjweVosgOkyKjRroSFB18KtHbVG5/ow6Y Xb4zNXhINhE2nbDpB+fmHwVtO04OFUN/qcIpX2gRRMuk2m3vyn+LJwEr/kWfCuV3 fBn5Qg3gdoD6sp9VeoMPja24ssYRPxVySzbtiIAiSUojD2CL0iY2KmXh1Ekp5t1f smjV7t/9tFoEZlaFfc/gTqvDnIL0prBvYWffjvPl8hu1VG3tLwS/RsfsDcTtENKf A== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 44mwwpjqs5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:42:58 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 517HZEAs022663; Fri, 7 Feb 2025 18:42:57 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 44j8ec8686-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:42:57 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 517Idx9P037660; Fri, 7 Feb 2025 18:42:56 GMT Received: from localhost.localdomain (ca-dev80.us.oracle.com [10.211.9.80]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 44j8ec8665-4; Fri, 07 Feb 2025 18:42:56 +0000 From: Dongli Zhang To: virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org Cc: mst@redhat.com, jasowang@redhat.com, eperezma@redhat.com, michael.christie@oracle.com, pbonzini@redhat.com, stefanha@redhat.com, joao.m.martins@oracle.com, joe.jin@oracle.com, si-wei.liu@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH 3/9] vhost-scsi: cache log buffer in I/O queue vhost_scsi_cmd Date: Fri, 7 Feb 2025 10:41:47 -0800 Message-ID: <20250207184212.20831-4-dongli.zhang@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250207184212.20831-1-dongli.zhang@oracle.com> References: <20250207184212.20831-1-dongli.zhang@oracle.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-02-07_08,2025-02-07_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 phishscore=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2501170000 definitions=main-2502070139 X-Proofpoint-ORIG-GUID: aRvRNdqrJq3lX0xQB3iyIbtwkC5CP6Bt X-Proofpoint-GUID: aRvRNdqrJq3lX0xQB3iyIbtwkC5CP6Bt The vhost-scsi I/O queue uses vhost_scsi_cmd. Pre-allocate the log buffer during vhost_scsi_cmd allocation, and free it when vhost_scsi_cmd is reclaimed. The cached log buffer will be uses in upcoming patches to log write descriptors for the I/O queue. The core idea is to cache the log in the per-command log buffer in the submission path, and use them to log write descriptors in the completion path. Suggested-by: Joao Martins Signed-off-by: Dongli Zhang --- drivers/vhost/scsi.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c index ee2310555740..5e6221cbbe9e 100644 --- a/drivers/vhost/scsi.c +++ b/drivers/vhost/scsi.c @@ -98,6 +98,11 @@ struct vhost_scsi_cmd { unsigned char tvc_cdb[VHOST_SCSI_MAX_CDB_SIZE]; /* Sense buffer that will be mapped into outgoing status */ unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER]; + /* + * Dirty write descriptors of this command. + */ + struct vhost_log *tvc_log; + unsigned int tvc_log_num; /* Completed commands list, serviced from vhost worker thread */ struct llist_node tvc_completion_list; /* Used to track inflight cmd */ @@ -619,6 +624,7 @@ vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg, struct vhost_scsi_nexus *tv_nexus; struct scatterlist *sg, *prot_sg; struct iovec *tvc_resp_iov; + struct vhost_log *log; struct page **pages; int tag; @@ -639,6 +645,7 @@ vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg, prot_sg = cmd->tvc_prot_sgl; pages = cmd->tvc_upages; tvc_resp_iov = cmd->tvc_resp_iov; + log = cmd->tvc_log; memset(cmd, 0, sizeof(*cmd)); cmd->tvc_sgl = sg; cmd->tvc_prot_sgl = prot_sg; @@ -652,6 +659,7 @@ vhost_scsi_get_cmd(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg, cmd->tvc_nexus = tv_nexus; cmd->inflight = vhost_scsi_get_inflight(vq); cmd->tvc_resp_iov = tvc_resp_iov; + cmd->tvc_log = log; memcpy(cmd->tvc_cdb, cdb, VHOST_SCSI_MAX_CDB_SIZE); @@ -1604,6 +1612,7 @@ static void vhost_scsi_destroy_vq_cmds(struct vhost_virtqueue *vq) kfree(tv_cmd->tvc_prot_sgl); kfree(tv_cmd->tvc_upages); kfree(tv_cmd->tvc_resp_iov); + kfree(tv_cmd->tvc_log); } sbitmap_free(&svq->scsi_tags); @@ -1666,6 +1675,18 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds) pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n"); goto out; } + + /* + * tv_cmd->tvc_log and vq->log need to have the same max + * length. + */ + tv_cmd->tvc_log = kcalloc(vq->dev->iov_limit, + sizeof(struct vhost_log), + GFP_KERNEL); + if (!tv_cmd->tvc_log) { + pr_err("Unable to allocate tv_cmd->tvc_log\n"); + goto out; + } } return 0; out: From patchwork Fri Feb 7 18:41:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongli Zhang X-Patchwork-Id: 13965584 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36F7E1A38F9; Fri, 7 Feb 2025 18:43:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.165.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953786; cv=none; b=pzlU+MDC38YbU0K91KJUt6UrkeJHrMsUFo21iyLjDSPUu5Edtxxlsisi4ylhRwD5vHUdGoAfXbIVrVebrCpTC6qIy/4CENAdiBuRHIrzwnWRxspcYzkESC2wMBUvO7MsRIliwDmfuCVCvxfBPdbiyalGxKBn19/h8yhryj9YTYU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953786; c=relaxed/simple; bh=ikD3WcT3bZbN966PZ81A9i/+I6kiRYH61Uq2okRMyFw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZHa67PaQgbOdC1QEQbmReveSmBOeFiW3m8744WKkuhkqW9B9Un/MfbAEvCw2+9g/q6eI70+DT97pwd2RHYYI//vjiBKfejB3FnrDIKGDEGmB6fuDAAT7AhkRFhG98tIpGOeNGhjN3AOXHoADKtYlUnT/DkmY8t35igKkkKtQH/k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=YSUgRQqp; arc=none smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="YSUgRQqp" Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 517HgPQM023796; Fri, 7 Feb 2025 18:42:59 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=MqIgg zXMS+FRLkCHvwW4YvogltdqmKg2UK+X1oTySD0=; b=YSUgRQqpUpOpGZ1VoCBG8 uxwJu6uvL17PTt3sE1tCW9+BS8FvrzHp/5DpL19rDWwNM0jn1Lx+IYIXPpr2uOp9 3WOwQSAOi6FI1aN9pgUPCit/gBk+cliRm5HYQrSEJ0QlLsaPJ30Lr8jXidDlDY3/ r3Chcvor1l6OKX7Dx4uEbpwkg1rtPNTjr2vfIGknU2lYBxr8YyFCLX+sVGOj2aHZ J6E/8n6mMID2aZRL4mkaaZsxtij9FIeWGiz4As1bJP54EPBG/tvQdKfFiSoGxhZT NK8ajkijwap58N72SBml61GLEyfSR9/1f94HZ1hWNOJvgfa25YC5luOeHHwlPT2Y A== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 44m58cnc7s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:42:58 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 517HPB40022524; Fri, 7 Feb 2025 18:42:58 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 44j8ec868p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:42:58 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 517Idx9R037660; Fri, 7 Feb 2025 18:42:57 GMT Received: from localhost.localdomain (ca-dev80.us.oracle.com [10.211.9.80]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 44j8ec8665-5; Fri, 07 Feb 2025 18:42:57 +0000 From: Dongli Zhang To: virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org Cc: mst@redhat.com, jasowang@redhat.com, eperezma@redhat.com, michael.christie@oracle.com, pbonzini@redhat.com, stefanha@redhat.com, joao.m.martins@oracle.com, joe.jin@oracle.com, si-wei.liu@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH 4/9] vhost-scsi: log I/O queue write descriptors Date: Fri, 7 Feb 2025 10:41:48 -0800 Message-ID: <20250207184212.20831-5-dongli.zhang@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250207184212.20831-1-dongli.zhang@oracle.com> References: <20250207184212.20831-1-dongli.zhang@oracle.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-02-07_08,2025-02-07_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 phishscore=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2501170000 definitions=main-2502070139 X-Proofpoint-ORIG-GUID: Ea15RNTTBc6ZMQ2GGGpAKu6Ia5Z8lgYz X-Proofpoint-GUID: Ea15RNTTBc6ZMQ2GGGpAKu6Ia5Z8lgYz Log write descriptors for the I/O queue, leveraging vhost_scsi_get_desc() and vhost_get_vq_desc() to retrieve the array of write descriptors to obtain the log buffer. In addition, introduce a vhost-scsi specific function to log vring descriptors. In this function, the 'partial' argument is set to false, and the 'len' argument is set to 0, because vhost-scsi always logs all pages shared by a vring descriptor. Add WARN_ON_ONCE() since vhost-scsi doesn't support VIRTIO_F_ACCESS_PLATFORM. Store the log buffer during the submission path and log it in the completion path. Logging is also required in the error handling path of the submission process. While the submission path is already protected by vq->mutex, the completion path also requires this lock for synchronization. Suggested-by: Joao Martins Signed-off-by: Dongli Zhang --- drivers/vhost/scsi.c | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c index 5e6221cbbe9e..d678eaf4ca68 100644 --- a/drivers/vhost/scsi.c +++ b/drivers/vhost/scsi.c @@ -329,6 +329,24 @@ static int vhost_scsi_check_prot_fabric_only(struct se_portal_group *se_tpg) return tpg->tv_fabric_prot_type; } +static void vhost_scsi_log_write(struct vhost_virtqueue *vq, + struct vhost_log *log, + unsigned int log_num) +{ + if (likely(!log || !log_num)) + return; + + if (likely(!vhost_has_feature(vq, VHOST_F_LOG_ALL))) + return; + + /* + * vhost-scsi doesn't support VIRTIO_F_ACCESS_PLATFORM. + * No requirement for vq->iotlb case. + */ + WARN_ON_ONCE(unlikely(vq->iotlb)); + vhost_log_write(vq, log, log_num, 0, false, NULL, 0); +} + static void vhost_scsi_release_cmd_res(struct se_cmd *se_cmd) { struct vhost_scsi_cmd *tv_cmd = container_of(se_cmd, @@ -606,6 +624,13 @@ static void vhost_scsi_complete_cmd_work(struct vhost_work *work) } else pr_err("Faulted on virtio_scsi_cmd_resp\n"); + if (unlikely(cmd->tvc_log_num)) { + mutex_lock(&cmd->tvc_vq->mutex); + vhost_scsi_log_write(cmd->tvc_vq, cmd->tvc_log, + cmd->tvc_log_num); + mutex_unlock(&cmd->tvc_vq->mutex); + } + vhost_scsi_release_cmd_res(se_cmd); } @@ -1082,6 +1107,8 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) u8 task_attr; bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI); void *cdb; + struct vhost_log *vq_log; + unsigned int log_num; mutex_lock(&vq->mutex); /* @@ -1097,8 +1124,11 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) vhost_disable_notify(&vs->dev, vq); + vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ? + vq->log : NULL; + do { - ret = vhost_scsi_get_desc(vs, vq, &vc, NULL, NULL); + ret = vhost_scsi_get_desc(vs, vq, &vc, vq_log, &log_num); if (ret) goto err; @@ -1238,6 +1268,11 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) cmd->tvc_resp_iov[i] = vq->iov[vc.out + i]; cmd->tvc_in_iovs = vc.in; + if (unlikely(vq_log && log_num)) { + memcpy(cmd->tvc_log, vq->log, sizeof(*cmd->tvc_log) * log_num); + cmd->tvc_log_num = log_num; + } + pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n", cmd->tvc_cdb[0], cmd->tvc_lun); pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:" @@ -1269,8 +1304,10 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) */ if (ret == -ENXIO) break; - else if (ret == -EIO) + else if (ret == -EIO) { vhost_scsi_send_bad_target(vs, vq, vc.head, vc.out); + vhost_scsi_log_write(vq, vq_log, log_num); + } } while (likely(!vhost_exceeds_weight(vq, ++c, 0))); out: mutex_unlock(&vq->mutex); From patchwork Fri Feb 7 18:41:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongli Zhang X-Patchwork-Id: 13965585 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B5121A5BA6; Fri, 7 Feb 2025 18:43:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.165.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953786; cv=none; b=bys8+6NrTYxwEA7JLX6+NhK8Aqw1Xt5KQeh5Tc8OeBFq3jbAZIj2wdZfJVWgq89UZXgmYMvXw+J+wMrAn4Gb1M3jVUXvm4b96IW64gqu3t6HFH7A8xvWqKKfbW9+1CMqn+J74HPFtgp2ICIfaldJTEnc39KxhhVgYV3BWTeFijU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953786; c=relaxed/simple; bh=zhRWT2vQ4ShDv7JFRICZwj/3J9l5AAj3S7LYGhL+nx8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oopUU5qr0pSVjOEUP5WNLY37YZKnVAGY1Q6c/ku5JnP+DTyDxOtM3rfdlxdGHzkqzbEZH1ZDnR7M/8DWc6Lyzt0/W8WeEnkqpwUnIOD3SDeBKXPrv3BkdtpYKBYnHEAbJjkXHs4oYs82mKNNMiOIRwGY0sA/zfGsjKioMsQtikg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=F24tFhzt; arc=none smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="F24tFhzt" Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 517Hfsul019896; Fri, 7 Feb 2025 18:42:59 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=H/rL7 aArIDYdrhLKz8f+nFnrm1Eb43CWtQQm+8sHeUU=; b=F24tFhztIuc04PThutJyA c8oUCrJeMJLyoax77i28RUa8dZjvhQ88ScyHlbIEk0uifDOwzs8JO9lQsc6rWaGb tqvyNWCsMN088S0XO//L/eAhfq1Dhe5mgSyjNMeB7vv6xWAQcYzEgDtZByxXiVJa q5DLgVfSldMjWs2ozCg3mV2m6ANDI7zjnU7fTiwiWVgLvdc2psXNYQhVhnM65eMn S3jL5TtDO45+tFUAZXZ+Pj4hxcvJMq3sEWuOu1Efq5AuH0To87HzoIzpOoXXu+9I KBhWDM/iPnZpJHJrywtN1KMvKqgwXadHCpPLQqhtL8yr3swLXSkSMfi+NJkzvMBb w== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 44n0fsjb2b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:42:59 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 517HPB41022524; Fri, 7 Feb 2025 18:42:58 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 44j8ec8695-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:42:58 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 517Idx9T037660; Fri, 7 Feb 2025 18:42:58 GMT Received: from localhost.localdomain (ca-dev80.us.oracle.com [10.211.9.80]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 44j8ec8665-6; Fri, 07 Feb 2025 18:42:58 +0000 From: Dongli Zhang To: virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org Cc: mst@redhat.com, jasowang@redhat.com, eperezma@redhat.com, michael.christie@oracle.com, pbonzini@redhat.com, stefanha@redhat.com, joao.m.martins@oracle.com, joe.jin@oracle.com, si-wei.liu@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH 5/9] vhost-scsi: log control queue write descriptors Date: Fri, 7 Feb 2025 10:41:49 -0800 Message-ID: <20250207184212.20831-6-dongli.zhang@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250207184212.20831-1-dongli.zhang@oracle.com> References: <20250207184212.20831-1-dongli.zhang@oracle.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-02-07_08,2025-02-07_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 phishscore=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2501170000 definitions=main-2502070139 X-Proofpoint-ORIG-GUID: OICwx9O4F2oe55ICQYpZB2btK8rBB9Cj X-Proofpoint-GUID: OICwx9O4F2oe55ICQYpZB2btK8rBB9Cj Log write descriptors for the control queue, leveraging vhost_scsi_get_desc() and vhost_get_vq_desc() to retrieve the array of write descriptors to obtain the log buffer. For Task Management Requests, similar to the I/O queue, store the log buffer during the submission path and log it in the completion or error handling path. For Asynchronous Notifications, only the submission path is involved. Suggested-by: Joao Martins Signed-off-by: Dongli Zhang --- drivers/vhost/scsi.c | 51 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c index d678eaf4ca68..21c2d07b806a 100644 --- a/drivers/vhost/scsi.c +++ b/drivers/vhost/scsi.c @@ -225,6 +225,12 @@ struct vhost_scsi_tmf { struct iovec resp_iov; int in_iovs; int vq_desc; + + /* + * Dirty write descriptors of this command. + */ + struct vhost_log *tmf_log; + unsigned int tmf_log_num; }; /* @@ -378,6 +384,11 @@ static void vhost_scsi_release_tmf_res(struct vhost_scsi_tmf *tmf) { struct vhost_scsi_inflight *inflight = tmf->inflight; + if (tmf->tmf_log_num) { + kfree(tmf->tmf_log); + tmf->tmf_log_num = 0; + } + kfree(tmf); vhost_scsi_put_inflight(inflight); } @@ -1348,6 +1359,14 @@ static void vhost_scsi_tmf_resp_work(struct vhost_work *work) vhost_scsi_send_tmf_resp(tmf->vhost, &tmf->svq->vq, tmf->in_iovs, tmf->vq_desc, &tmf->resp_iov, resp_code); + + if (unlikely(tmf->tmf_log_num)) { + mutex_lock(&tmf->svq->vq.mutex); + vhost_scsi_log_write(&tmf->svq->vq, tmf->tmf_log, + tmf->tmf_log_num); + mutex_unlock(&tmf->svq->vq.mutex); + } + vhost_scsi_release_tmf_res(tmf); } @@ -1369,7 +1388,8 @@ static void vhost_scsi_handle_tmf(struct vhost_scsi *vs, struct vhost_scsi_tpg *tpg, struct vhost_virtqueue *vq, struct virtio_scsi_ctrl_tmf_req *vtmf, - struct vhost_scsi_ctx *vc) + struct vhost_scsi_ctx *vc, + struct vhost_log *log, unsigned int log_num) { struct vhost_scsi_virtqueue *svq = container_of(vq, struct vhost_scsi_virtqueue, vq); @@ -1397,6 +1417,16 @@ vhost_scsi_handle_tmf(struct vhost_scsi *vs, struct vhost_scsi_tpg *tpg, tmf->in_iovs = vc->in; tmf->inflight = vhost_scsi_get_inflight(vq); + if (unlikely(log && log_num)) { + tmf->tmf_log = kmalloc_array(log_num, sizeof(*tmf->tmf_log), + GFP_KERNEL); + if (tmf->tmf_log) { + memcpy(tmf->tmf_log, log, sizeof(*tmf->tmf_log) * log_num); + tmf->tmf_log_num = log_num; + } else + pr_err("vhost_scsi tmf log allocation error\n"); + } + if (target_submit_tmr(&tmf->se_cmd, tpg->tpg_nexus->tvn_se_sess, NULL, vhost_buf_to_lun(vtmf->lun), NULL, TMR_LUN_RESET, GFP_KERNEL, 0, @@ -1410,6 +1440,7 @@ vhost_scsi_handle_tmf(struct vhost_scsi *vs, struct vhost_scsi_tpg *tpg, send_reject: vhost_scsi_send_tmf_resp(vs, vq, vc->in, vc->head, &vq->iov[vc->out], VIRTIO_SCSI_S_FUNCTION_REJECTED); + vhost_scsi_log_write(vq, log, log_num); } static void @@ -1446,6 +1477,8 @@ vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) struct vhost_scsi_ctx vc; size_t typ_size; int ret, c = 0; + struct vhost_log *vq_log; + unsigned int log_num; mutex_lock(&vq->mutex); /* @@ -1459,8 +1492,11 @@ vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) vhost_disable_notify(&vs->dev, vq); + vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ? + vq->log : NULL; + do { - ret = vhost_scsi_get_desc(vs, vq, &vc, NULL, NULL); + ret = vhost_scsi_get_desc(vs, vq, &vc, vq_log, &log_num); if (ret) goto err; @@ -1524,9 +1560,12 @@ vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) goto err; if (v_req.type == VIRTIO_SCSI_T_TMF) - vhost_scsi_handle_tmf(vs, tpg, vq, &v_req.tmf, &vc); - else + vhost_scsi_handle_tmf(vs, tpg, vq, &v_req.tmf, &vc, + vq_log, log_num); + else { vhost_scsi_send_an_resp(vs, vq, &vc); + vhost_scsi_log_write(vq, vq_log, log_num); + } err: /* * ENXIO: No more requests, or read error, wait for next kick @@ -1536,8 +1575,10 @@ vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) */ if (ret == -ENXIO) break; - else if (ret == -EIO) + else if (ret == -EIO) { vhost_scsi_send_bad_target(vs, vq, vc.head, vc.out); + vhost_scsi_log_write(vq, vq_log, log_num); + } } while (likely(!vhost_exceeds_weight(vq, ++c, 0))); out: mutex_unlock(&vq->mutex); From patchwork Fri Feb 7 18:41:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongli Zhang X-Patchwork-Id: 13965588 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 656CB1AAA10; Fri, 7 Feb 2025 18:43:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.165.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953787; cv=none; b=To5mhA0eM2z+UTk1zpnoYP+1EF0a9cVlkA0PnzIe+fqZb8D6Lm2G9uVLDAxPJ0tB1m8V2XN0ST5Nw6LUO3aHIkS/5wCgULGVim+fSlnIAReDeTHEXofdMocmN9UZtnTc5twu60JCpsTOOT3hwue+hiN1mMC3GYqnwZtosX4jQPk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953787; c=relaxed/simple; bh=cPAdDcIzCEKs56dgAaNj1MQuvQzaDRfcPqE4lVRK/ms=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dzU1czON4J+xJ9uCWBihrYu4CpewxP5GDLBWrqltXcU1/zWXiVIw7saL7gpaZ7eodG47jLvYA4Q8P28xJx9OeOXpvN9g4Lm6vOlzkR6a2u+iKB4yfEaHcpWYsO/GPaDjIPnookS7doqonkP4HxlYfMG1c20X3Wg2L6eJvwwIJf0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=R3z/2E48; arc=none smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="R3z/2E48" Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 517Hg7KH000629; Fri, 7 Feb 2025 18:43:00 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=Dmes3 R9tkzYX7VMGKXll9aiulkQtaayDUW51RCRhbDM=; b=R3z/2E481tEaXcSwPkuXZ WPGoprAwAUpCqyMGqoYI4FYdcEHNlOnTmyNOwlx2mRyx8emF5K77a9S+kvU+9Eqp 9/ndSfNeu9TWlWJRZtogxm7pdHkTcElND7hxCcZDZpdow2oISegJDoD27zT/tvv/ 2HDw/nPd7BOMtRmZm+SN4+PQGP0BO2SNi5qStIaRYr2b214+D/c/w/KLVCpvXMGx 7YluFE108ezECsDIolZH8F1dOUvoQITXtefUr5DStr6SBA+8MVposwMHmb8b1Gye nVRMuYA1jusuyIVyhpQvi6q3xPpUDV8/8XZvM6AUPTOv+zOrkfWLPyu8Ih1cl0Ep w== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 44mqk8bbh9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:43:00 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 517HPB42022524; Fri, 7 Feb 2025 18:42:59 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 44j8ec869r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:42:59 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 517Idx9V037660; Fri, 7 Feb 2025 18:42:59 GMT Received: from localhost.localdomain (ca-dev80.us.oracle.com [10.211.9.80]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 44j8ec8665-7; Fri, 07 Feb 2025 18:42:59 +0000 From: Dongli Zhang To: virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org Cc: mst@redhat.com, jasowang@redhat.com, eperezma@redhat.com, michael.christie@oracle.com, pbonzini@redhat.com, stefanha@redhat.com, joao.m.martins@oracle.com, joe.jin@oracle.com, si-wei.liu@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH 6/9] vhost-scsi: log event queue write descriptors Date: Fri, 7 Feb 2025 10:41:50 -0800 Message-ID: <20250207184212.20831-7-dongli.zhang@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250207184212.20831-1-dongli.zhang@oracle.com> References: <20250207184212.20831-1-dongli.zhang@oracle.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-02-07_08,2025-02-07_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 phishscore=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2501170000 definitions=main-2502070139 X-Proofpoint-GUID: UvnJWz_xjjUFu1lhQBiwxCCLEbFsljie X-Proofpoint-ORIG-GUID: UvnJWz_xjjUFu1lhQBiwxCCLEbFsljie Log write descriptors for the event queue, leveraging vhost_get_vq_desc() to retrieve the array of write descriptors to obtain the log buffer. There is only one path for event queue. Suggested-by: Joao Martins Signed-off-by: Dongli Zhang --- drivers/vhost/scsi.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c index 21c2d07b806a..40268b88f470 100644 --- a/drivers/vhost/scsi.c +++ b/drivers/vhost/scsi.c @@ -498,6 +498,8 @@ vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt) struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; struct virtio_scsi_event *event = &evt->event; struct virtio_scsi_event __user *eventp; + struct vhost_log *vq_log; + unsigned int log_num; unsigned out, in; int head, ret; @@ -508,9 +510,19 @@ vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt) again: vhost_disable_notify(&vs->dev, vq); + + vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ? + vq->log : NULL; + + /* + * Reset 'log_num' since vhost_get_vq_desc() may reset it only + * after certain condition checks. + */ + log_num = 0; + head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov), &out, &in, - NULL, NULL); + vq_log, &log_num); if (head < 0) { vs->vs_events_missed = true; return; @@ -540,6 +552,8 @@ vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt) vhost_add_used_and_signal(&vs->dev, vq, head, 0); else vq_err(vq, "Faulted on vhost_scsi_send_event\n"); + + vhost_scsi_log_write(vq, vq_log, log_num); } static void vhost_scsi_complete_events(struct vhost_scsi *vs, bool drop) From patchwork Fri Feb 7 18:41:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongli Zhang X-Patchwork-Id: 13965589 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81CD21B415B; Fri, 7 Feb 2025 18:43:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.177.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953788; cv=none; b=UAXqOx4tVsCvp0F1+xsxhCaWoCBJQ/7O5U63scP2ZQSIR7IA+B54NbkyUHswtfOPFb1gza0UYYxl994lID6/ZGlZtdcpIK1rb3ivbR7SpaEknyxBcfyk5ufy6AsoIb9kCf9GSBRjVh9AVqAcXT8s3wjfh/DGs+9Tx32ccF/kFiM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953788; c=relaxed/simple; bh=fwr8OqvAlKugsufZvHpZOeNEgA7dt65mCk1pC0dH1Nk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TECUEjv9FU64Q9N1fR077F3Ohkfh8TASx9rYYXS6VS/V4SPk5LEgJUCZ93Yz9tEn3DavT3DfQedl2J+gSAd6lbLdXwYBILcQDnzPahiZ2GndFg7vo6RCVuZiTYNE52Lc7+lIopO2apnq6oiKC607o63bQIY8uMrBjeRtBG5WHz4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=ngGWZne0; arc=none smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="ngGWZne0" Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 517HfrNw009275; Fri, 7 Feb 2025 18:43:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=3NjNL hMvEyqe/yXAgspbGPylieUE1KvOmQHbOi6+uLk=; b=ngGWZne0VAfmZs0sBiowD zBTtIC3EozJ3CqfTWgJqRL0GZHEC3X/nqrU9FjVnMjl3rUt5R3VXa9a46n5qkgLd 1UwLaeT84jkUU/ZTFu5vNXi3iliBFBtXlP0B6Km4RyUjO3VYYP1FTu9E5fy3PdeG +0BIlfQ3Mf/SeU39EjFsuFpuCWDbb0X/d/KHBmDJUA2/+FZjqfVdoqsVAETWbOt+ HFguAPZFL0WrTkIRGTi+x6gEK03lEJM9meAE2tcQE2hySO+ALxafWWeJwSwRvU83 jSB2sHfS0ocXEnjQLYhc7XDsCwsAICGdchmhvYTtvB+D9mqtC+2wPeg3l9QZWGca g== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 44mwwpjqsf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:43:01 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 517Hbxg0022695; Fri, 7 Feb 2025 18:43:00 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 44j8ec86a4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:43:00 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 517Idx9X037660; Fri, 7 Feb 2025 18:43:00 GMT Received: from localhost.localdomain (ca-dev80.us.oracle.com [10.211.9.80]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 44j8ec8665-8; Fri, 07 Feb 2025 18:42:59 +0000 From: Dongli Zhang To: virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org Cc: mst@redhat.com, jasowang@redhat.com, eperezma@redhat.com, michael.christie@oracle.com, pbonzini@redhat.com, stefanha@redhat.com, joao.m.martins@oracle.com, joe.jin@oracle.com, si-wei.liu@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH 7/9] vhost: add WARNING if log_num is more than limit Date: Fri, 7 Feb 2025 10:41:51 -0800 Message-ID: <20250207184212.20831-8-dongli.zhang@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250207184212.20831-1-dongli.zhang@oracle.com> References: <20250207184212.20831-1-dongli.zhang@oracle.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-02-07_08,2025-02-07_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 phishscore=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2501170000 definitions=main-2502070139 X-Proofpoint-ORIG-GUID: Aa41DM9V3T4iNcwENIKSysJOHJhVkliw X-Proofpoint-GUID: Aa41DM9V3T4iNcwENIKSysJOHJhVkliw Since long time ago, the only user of vq->log is vhost-net. The concern is to add support for more devices (i.e. vhost-scsi or vsock) may reveals unknown issue in the vhost API. Add a WARNING. Suggested-by: Joao Martins Signed-off-by: Dongli Zhang --- drivers/vhost/vhost.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index db3b30aba940..8368370b40f7 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2553,6 +2553,15 @@ static int get_indirect(struct vhost_virtqueue *vq, if (access == VHOST_ACCESS_WO) { *in_num += ret; if (unlikely(log && ret)) { + /* + * Since long time ago, the only user of + * vq->log is vhost-net. The concern is to + * add support for more devices (i.e. + * vhost-scsi or vsock) may reveals unknown + * issue in the vhost API. Add a WARNING. + */ + WARN_ON_ONCE(*log_num >= vq->dev->iov_limit); + log[*log_num].addr = vhost64_to_cpu(vq, desc.addr); log[*log_num].len = vhost32_to_cpu(vq, desc.len); ++*log_num; @@ -2673,6 +2682,15 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, * increment that count. */ *in_num += ret; if (unlikely(log && ret)) { + /* + * Since long time ago, the only user of + * vq->log is vhost-net. The concern is to + * add support for more devices (i.e. + * vhost-scsi or vsock) may reveals unknown + * issue in the vhost API. Add a WARNING. + */ + WARN_ON_ONCE(*log_num >= vq->dev->iov_limit); + log[*log_num].addr = vhost64_to_cpu(vq, desc.addr); log[*log_num].len = vhost32_to_cpu(vq, desc.len); ++*log_num; From patchwork Fri Feb 7 18:41:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongli Zhang X-Patchwork-Id: 13965590 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED6361B87D3; Fri, 7 Feb 2025 18:43:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.165.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953791; cv=none; b=Ae/E1rfyTYY93zSHJcq5LYbCPRUlUdXGYrKn3VSXLwTPsxgRflqoLmwDCoIM4al1hccT66vVnLnX1GcYvuKCDdK/67CPVZ26I1pIxxrWcj3Kzm2R7bqPHn7ARfkgK9OuLwXGlZc5o1Rv9MazrRKWGcBPkAEaeP1FyYMsr9PI0a8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953791; c=relaxed/simple; bh=TheOBRcnGVBP3+zBn1fo/KIGp8ZORIbEsBySJSBTXRM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DIxOGbe+YFaUycqsto5Qx7hdzNwkQpoosIQCgBf7YYXbzGL5i0Xrz6Z9a+5rJbw8cQAYFZOQglf7cqYTRrvGIp5va+7UuIuo4E9VbKZYeIdu45Xwyu1UbIeXOsjhyjruRVh3zO1eM8CWchp6F+zUTHv6gCteAw6dRifEyoAmQ24= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=N+4NwIw5; arc=none smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="N+4NwIw5" Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 517Hg6XQ000481; Fri, 7 Feb 2025 18:43:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=0f86d riItZ6Voi0DN/wvEX77XPRc7/eHLKbBCszuiEc=; b=N+4NwIw5PBSjAHyk6ALR+ pe4ATEBVeT4lpSXndz9zZKXZ1SgrQQkbCbHzJSMNBNTWULJCYzEVQhi1j04l5VPz ggiH4yteJjFkMDRdBXOI8KNXxzkcyQBVwCPvVsa8bPde7qyWjKG2xBrSfQAt0v3A sghk1f6QVHeTNG/Zlp1UYyIaDWkH4q4C9FBp3eYYR0vB8uMCQQaCZf6zwN7jXGl7 gwprXXfkCWekxv/2gUowJEV7mCwwIZs3ZzXkQh+h3S+l/F485X9jcRF2UYNW+FF/ wfjV6lguJb6kb0t6Dvm0XeIXFK0tbUtNaSwYysNGZex7QZaO0mHXLzdnaHsKC68f g== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 44mqk8bbhh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:43:01 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 517HTvB2022565; Fri, 7 Feb 2025 18:43:01 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 44j8ec86ar-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:43:01 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 517Idx9Z037660; Fri, 7 Feb 2025 18:43:00 GMT Received: from localhost.localdomain (ca-dev80.us.oracle.com [10.211.9.80]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 44j8ec8665-9; Fri, 07 Feb 2025 18:43:00 +0000 From: Dongli Zhang To: virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org Cc: mst@redhat.com, jasowang@redhat.com, eperezma@redhat.com, michael.christie@oracle.com, pbonzini@redhat.com, stefanha@redhat.com, joao.m.martins@oracle.com, joe.jin@oracle.com, si-wei.liu@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH 8/9] vhost-scsi: protect vq->log_used with vq->mutex Date: Fri, 7 Feb 2025 10:41:52 -0800 Message-ID: <20250207184212.20831-9-dongli.zhang@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250207184212.20831-1-dongli.zhang@oracle.com> References: <20250207184212.20831-1-dongli.zhang@oracle.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-02-07_08,2025-02-07_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 phishscore=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2501170000 definitions=main-2502070139 X-Proofpoint-GUID: fI64uTZGsKRvKXVVXcvMNBOXPI3Lm_Wb X-Proofpoint-ORIG-GUID: fI64uTZGsKRvKXVVXcvMNBOXPI3Lm_Wb The vhost-scsi completion path may access vq->log_base when vq->log_used is already set to false. vhost-thread QEMU-thread vhost_scsi_complete_cmd_work() -> vhost_add_used() -> vhost_add_used_n() if (unlikely(vq->log_used)) QEMU disables vq->log_used via VHOST_SET_VRING_ADDR. mutex_lock(&vq->mutex); vq->log_used = false now! mutex_unlock(&vq->mutex); QEMU gfree(vq->log_base) log_used() -> log_write(vq->log_base) Assuming the VMM is QEMU. The vq->log_base is from QEMU userpace and can be reclaimed via gfree(). As a result, this causes invalid memory writes to QEMU userspace. The control queue path has the same issue. Signed-off-by: Dongli Zhang --- drivers/vhost/scsi.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c index 40268b88f470..3b87d698adaf 100644 --- a/drivers/vhost/scsi.c +++ b/drivers/vhost/scsi.c @@ -645,7 +645,9 @@ static void vhost_scsi_complete_cmd_work(struct vhost_work *work) if (likely(ret == sizeof(v_rsp))) { signal = true; + mutex_lock(&cmd->tvc_vq->mutex); vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0); + mutex_unlock(&cmd->tvc_vq->mutex); } else pr_err("Faulted on virtio_scsi_cmd_resp\n"); @@ -1371,8 +1373,10 @@ static void vhost_scsi_tmf_resp_work(struct vhost_work *work) else resp_code = VIRTIO_SCSI_S_FUNCTION_REJECTED; + mutex_lock(&tmf->svq->vq.mutex); vhost_scsi_send_tmf_resp(tmf->vhost, &tmf->svq->vq, tmf->in_iovs, tmf->vq_desc, &tmf->resp_iov, resp_code); + mutex_unlock(&tmf->svq->vq.mutex); if (unlikely(tmf->tmf_log_num)) { mutex_lock(&tmf->svq->vq.mutex); From patchwork Fri Feb 7 18:41:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongli Zhang X-Patchwork-Id: 13965591 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D72D1DE3BF; Fri, 7 Feb 2025 18:43:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.177.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953791; cv=none; b=MUjZ2Aq3Ht48yaEbXsjTNZ7Gq0pqkXBwKaFD/2siS8rZgYSke85UDHgWrFj4RBmuf7DClg32WFP7K7QknAq51L/q6sYziL7BUN06UxC1sGCoLRbJNHnhby4itvG144J1vhLLw12iZ5dmPpJKqfsy5KNv/WZVPIaaY4FdfFKtMpk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738953791; c=relaxed/simple; bh=nQnZYW52vb86coFPjs/TEXCK0Op+JRWUcCW/VbtdSps=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rpuyYd5SXxOtxgLRfJuRJbgmKhu4ouuvIAwsBH6JuHjSZygSyxgN1ZNKXHOcs9HDZBscqm0dENUikLdOZfVK4zUbxg2TSW2JeuAfFyFl20UcwFJ2zRRk5TujYBSStHe9RVJSxLhlF6DydTBV8dEUHB1xujN2ASqQqn2hI8T76bU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=GjTBR9p0; arc=none smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="GjTBR9p0" Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 517HfsNZ011643; Fri, 7 Feb 2025 18:43:03 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=Ketic Cnpyb6jzdEUojEwLtoo1PFBDN83aIYMiT5obl4=; b=GjTBR9p0xeWArPk1s37Au 5sIrtaE+u3yb8U1viPRg7anBbUsjTbNwb1so7Y5/vezhlwCdQ0RmhxzujoIPO8s2 vLJBZlCAt5zL3TmXC8Yd1hHqHySd1G4WGdRebDGTp3FSPHIRv4Ns9VrCIrcjV61l kPgXTBge6TISpaCiKZgQfRMiFEHiXOBZy4mnVJRVCCHeCVX/2RKy+4PEJRznsy0W CJDhdyaHTeOcjjSURvVlLgcW3hiAgQSkot7HSScvEwrhH8sCJ82cvMIvUjXXqZmG iv3srz5BoDEmnGYx33Jtek38HqAgh5zlrkUcuyU1+aZERiokOdtHgoDPWwfKegku g== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 44m50udacn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:43:03 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 517HKTTq022731; Fri, 7 Feb 2025 18:43:02 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 44j8ec86b7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 07 Feb 2025 18:43:02 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 517Idx9b037660; Fri, 7 Feb 2025 18:43:01 GMT Received: from localhost.localdomain (ca-dev80.us.oracle.com [10.211.9.80]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 44j8ec8665-10; Fri, 07 Feb 2025 18:43:01 +0000 From: Dongli Zhang To: virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org Cc: mst@redhat.com, jasowang@redhat.com, eperezma@redhat.com, michael.christie@oracle.com, pbonzini@redhat.com, stefanha@redhat.com, joao.m.martins@oracle.com, joe.jin@oracle.com, si-wei.liu@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH 9/9] vhost-scsi: Fix vhost_scsi_send_bad_target() Date: Fri, 7 Feb 2025 10:41:53 -0800 Message-ID: <20250207184212.20831-10-dongli.zhang@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250207184212.20831-1-dongli.zhang@oracle.com> References: <20250207184212.20831-1-dongli.zhang@oracle.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-02-07_08,2025-02-07_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 phishscore=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2501170000 definitions=main-2502070139 X-Proofpoint-GUID: cg1KL6VSm7q0IoyM47cXu2dE5uwnNUjn X-Proofpoint-ORIG-GUID: cg1KL6VSm7q0IoyM47cXu2dE5uwnNUjn Although the support of VIRTIO_F_ANY_LAYOUT + VIRTIO_F_VERSION_1 was signaled by the commit 664ed90e621c ("vhost/scsi: Set VIRTIO_F_ANY_LAYOUT + VIRTIO_F_VERSION_1 feature bits"), vhost_scsi_send_bad_target() still assumes the response in a single descriptor. In addition, although vhost_scsi_send_bad_target() is used by both I/O queue and control queue, the response header is always virtio_scsi_cmd_resp. It is required to use virtio_scsi_ctrl_tmf_resp or virtio_scsi_ctrl_an_resp for control queue. Fixes: 664ed90e621c ("vhost/scsi: Set VIRTIO_F_ANY_LAYOUT + VIRTIO_F_VERSION_1 feature bits") Signed-off-by: Dongli Zhang --- drivers/vhost/scsi.c | 50 +++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c index 3b87d698adaf..6aa3f3ad2695 100644 --- a/drivers/vhost/scsi.c +++ b/drivers/vhost/scsi.c @@ -989,23 +989,46 @@ static void vhost_scsi_target_queue_cmd(struct vhost_scsi_cmd *cmd) target_submit(se_cmd); } +#define TYPE_IO_CMD 0 +#define TYPE_CTRL_TMF 1 +#define TYPE_CTRL_AN 2 + static void vhost_scsi_send_bad_target(struct vhost_scsi *vs, struct vhost_virtqueue *vq, - int head, unsigned out) + struct vhost_scsi_ctx *vc, int type) { - struct virtio_scsi_cmd_resp __user *resp; - struct virtio_scsi_cmd_resp rsp; + union { + struct virtio_scsi_cmd_resp cmd; + struct virtio_scsi_ctrl_tmf_resp tmf; + struct virtio_scsi_ctrl_an_resp an; + } resp; + struct iov_iter iov_iter; + size_t resp_size; int ret; - memset(&rsp, 0, sizeof(rsp)); - rsp.response = VIRTIO_SCSI_S_BAD_TARGET; - resp = vq->iov[out].iov_base; - ret = __copy_to_user(resp, &rsp, sizeof(rsp)); - if (!ret) - vhost_add_used_and_signal(&vs->dev, vq, head, 0); + memset(&resp, 0, sizeof(resp)); + + if (type == TYPE_IO_CMD) { + resp_size = sizeof(struct virtio_scsi_cmd_resp); + resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET; + } else if (type == TYPE_CTRL_TMF) { + resp_size = sizeof(struct virtio_scsi_ctrl_tmf_resp); + resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET; + } else { + resp_size = sizeof(struct virtio_scsi_ctrl_an_resp); + resp.an.response = VIRTIO_SCSI_S_BAD_TARGET; + } + + iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in, + resp_size); + + ret = copy_to_iter(&resp, resp_size, &iov_iter); + + if (likely(ret == resp_size)) + vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0); else - pr_err("Faulted on virtio_scsi_cmd_resp\n"); + pr_err("Faulted on virtio scsi type=%d\n", type); } static int @@ -1332,7 +1355,7 @@ vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) if (ret == -ENXIO) break; else if (ret == -EIO) { - vhost_scsi_send_bad_target(vs, vq, vc.head, vc.out); + vhost_scsi_send_bad_target(vs, vq, &vc, TYPE_IO_CMD); vhost_scsi_log_write(vq, vq_log, log_num); } } while (likely(!vhost_exceeds_weight(vq, ++c, 0))); @@ -1594,7 +1617,10 @@ vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq) if (ret == -ENXIO) break; else if (ret == -EIO) { - vhost_scsi_send_bad_target(vs, vq, vc.head, vc.out); + vhost_scsi_send_bad_target(vs, vq, &vc, + v_req.type == VIRTIO_SCSI_T_TMF ? + TYPE_CTRL_TMF : + TYPE_CTRL_AN); vhost_scsi_log_write(vq, vq_log, log_num); } } while (likely(!vhost_exceeds_weight(vq, ++c, 0)));