From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 54ECF3382EC for ; Wed, 3 Jun 2026 05:10:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780463419; cv=none; b=StzP491LjoMbvk3HxuFXjKqsxIYTQw5RU/IZGSd4QwBjz8BOHBFziqe0PWe0vv09EfSwiUlsp60fEGDZgU/FEuQ7XXhNvT4OU5KV/pCbectRPFonIqDJ58aivPe0CzdfChFAWAIk2uLHpAT98KK5bdfsof+reLVaiahQmKqG44w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780463419; c=relaxed/simple; bh=TMuRorPnXh0/q0Epr8aWbRUQFdWaiTfi93eQL9g5Ftg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=RQnDveySOkoNyb+w2NEz7eVOQ1nreQGz3gZNLhZ1HmtaJrugJh9bnSZfK5sjGXERYFAQCNlI50hcocNbG021AKUHuhz7EVz4/os52UXB+F9mzUP78npITC0CaF0guGemRTaKY3CogfKBUCDGTjy+IbXUp31j9bufcNjlKwMUEhM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=cXpN1YYN; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=HCyCr6/X; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="cXpN1YYN"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="HCyCr6/X" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780463417; 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=r+J3prPmslsfPO9pmuIRUDYHV1FwtfpJ+bwDKkD+iUA=; b=cXpN1YYN23IQaRFbEsPifv283V6ZR/K7zn0PW7Z4MeaX3QHenz6tiM6ANzhYecRBp4IiMC /hORdnB+oG4llPQUL3oWr9H7Ri+fYqmhILKaOLJSjt6c/4delOXj9mVtX7fuP+ugDGyt9m hXgoBdOQy7j36XkCBHH2gPYQhsTeigw= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-86-ansfijBxN0m_hby44yXBcw-1; Wed, 03 Jun 2026 01:10:16 -0400 X-MC-Unique: ansfijBxN0m_hby44yXBcw-1 X-Mimecast-MFC-AGG-ID: ansfijBxN0m_hby44yXBcw_1780463415 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-45eef10d5ebso2812815f8f.0 for ; Tue, 02 Jun 2026 22:10:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1780463415; x=1781068215; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=r+J3prPmslsfPO9pmuIRUDYHV1FwtfpJ+bwDKkD+iUA=; b=HCyCr6/XvsndWwOQudxB9BvL4WnpaAIS2ygW3p+tnchQ1jolSTygXXt2HgIYx8VnnG 0uWcdiG2PFuiuIaoVXkI967G6CG4cDrNppGyFn6PP63+9AIt9WwWR/Oai8zzc1A8AJcK dv3O9MBcFJnmbc7jKMGmCLVu9GceIA3SxC0YyF2XLXMqnr+zHtqG5SnJXaEeNhGiB/uN GqH0KWxBBtcPB0NXuwiD7s2ZohCnBF9BGAdG1rXnUjxkWR0yEZyhekFlsM4gCeTGuk2q rkRwfRDn37eba2/VFHY8PM0H5NEBotLMjGgaQ+G/pSH0D0CEXN6kVTl8W2xrnsvnfdvn Z+pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780463415; x=1781068215; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=r+J3prPmslsfPO9pmuIRUDYHV1FwtfpJ+bwDKkD+iUA=; b=ZuKrtV3V1U0jP+vKr/iMpdNxhj3kMggApmFQEtc9N6FfMv1yHwFiFri1caxBSEElnP GO/wmFIUTQtl7K12uXVuuKgoSQ4Z9PtK03/1Y0zybU37EFaGMqZteBeiw3FHCkP6SykE RNLNcwTsT3BShvTBU5Yi4t6WTUcX+C0bqkv30y+Vbl1E8A0lJCyv7266mTckD1mvHivj KDBWNkUuavFphq2GGwtPh8/dnvJ1inyvoDUZ7+qZQCG1IoNovhLnUk0nzVad1g4pZ/B+ PV29zn5ThzCMp8aC8nQ+NoCv4A8RZePyDPMiWM37rRJXXJ3Q1mcwSukUN15ewDhs+km2 UzoA== X-Forwarded-Encrypted: i=1; AFNElJ8MQpqVjJZqah+RqyQsglyiUC/TYxKn1V/YL6hkeAzlfxLU6lotuaab8ttcPn1rc5SUckkxQ2KwkGzUdwk=@vger.kernel.org X-Gm-Message-State: AOJu0YwUA10ktRarAh5mu2tWHyvCVqZf6oAoKF0vpfnUGTW9dA1+fa/f c3CPDFnrz1gpskzBhv8NrkiKo2Xmn61Xf50tsPwAsqRRMhAvVZFkwMmCfpgn5CpPFw1tBnTIyCo PMYYIsxPzfqXlXIlLGTSX7p7XiugUxW26XB2VvPcLd72qlicIxbUIkEd6/RoIxeP5VQ== X-Gm-Gg: Acq92OED7ECBXLkF5HdI/nmGl4/8kN5IfRMridh269oLQP2PIUkqSoy8251NVZVOFS9 EskEE8cozzaucB/CMy08XT96Rn+m8Uq8ir6cFzwN/G9zob7P39W9yvd00NdSdNWbuyBY/kr9Tbd Ct1yWesETfguDgltEKR+2dAAuWJ3ohpH27l5Em/Mkg5vGjTIjmv9H9YHoNkyXa2INZa3R+/jeeu moMpGNu94m5cF4Ij+CZAOi96Sl4/ctOHegbTCw9AdI9JiIywCJbrMv5MJNTXu32QTsMzlPxH0lr JDcI0McdhUPKK6REseUNmZG/i6twSUlhjtU7hhAmj8h8ZKSHKULYwcFhxWEJFSrqd9e02NLV+47 wMny5Mw1/gL6sNCs2ycA4pX7L660ifB0ZgisOWS7x1GraW0YWgKbueg== X-Received: by 2002:a5d:42c2:0:b0:45e:e230:e53 with SMTP id ffacd0b85a97d-460218701f1mr1573480f8f.34.1780463414691; Tue, 02 Jun 2026 22:10:14 -0700 (PDT) X-Received: by 2002:a5d:42c2:0:b0:45e:e230:e53 with SMTP id ffacd0b85a97d-460218701f1mr1573433f8f.34.1780463414129; Tue, 02 Jun 2026 22:10:14 -0700 (PDT) Received: from redhat.com (IGLD-80-230-25-45.inter.net.il. [80.230.25.45]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4601f0a43e9sm4549179f8f.0.2026.06.02.22.10.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jun 2026 22:10:13 -0700 (PDT) Date: Wed, 3 Jun 2026 01:10:10 -0400 From: "Michael S. Tsirkin" To: yangjiale133 Cc: Eugenio Perez Martin , Jason Wang , Xuan Zhuo , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: Re: [PATCH] VIRTIO: Update the desc 'flag' fied last in packed ring. Message-ID: <20260603010910-mutt-send-email-mst@kernel.org> References: <20260602043123.10207-1-yangjiale133@163.com> <5a3e06d5.103d.19e8b1863dd.Coremail.yangjiale133@163.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <5a3e06d5.103d.19e8b1863dd.Coremail.yangjiale133@163.com> On Wed, Jun 03, 2026 at 09:28:11AM +0800, yangjiale133 wrote: > From the device's perspective, during a single read of the descriptor list > specifically when that list spans across cache lines. > the retrieved data will show `desc[head].flags` as valid, > and `desc[i].flags` as valid as well; however, > the `desc[i].addr` and `len` fields may be invalid. > > I apologize that I currently lack the necessary environment to verify > whether this modification definitively resolves the issue or > merely reduces the probability of its occurrence; > therefore, this patch can be discarded. > > yangjiale it could be that your device does a single read of the descriptor. the pci spec is explicit that after getting valid flags, device must read the descriptor again. > > At 2026-06-02 14:04:13, "Eugenio Perez Martin" wrote: > >On Tue, Jun 2, 2026 at 6:34 AM yangjiale wrote: > >> > >> When a descriptor list spans across cache lines, > >> updating the flag first can lead to a scenario where the device side > >> perceives the flag as valid, yet the corresponding address and length > >> fields remain unupdated—resulting in invalid values. > >> Therefore, the flag field must be updated last. > >> > >> Signed-off-by: yangjiale > >> --- > >> drivers/virtio/virtio_ring.c | 8 ++++---- > >> 1 file changed, 4 insertions(+), 4 deletions(-) > >> > >> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c > >> index fbca7ce1c6bf..036b4f90d30f 100644 > >> --- a/drivers/virtio/virtio_ring.c > >> +++ b/drivers/virtio/virtio_ring.c > >> @@ -1688,6 +1688,10 @@ static inline int virtqueue_add_packed(struct vring_virtqueue *vq, > >> &addr, &len, premapped, attr)) > >> goto unmap_release; > >> > >> + desc[i].addr = cpu_to_le64(addr); > >> + desc[i].len = cpu_to_le32(len); > >> + desc[i].id = cpu_to_le16(id); > >> + > >> flags = cpu_to_le16(vq->packed.avail_used_flags | > >> (++c == total_sg ? 0 : VRING_DESC_F_NEXT) | > >> (n < out_sgs ? 0 : VRING_DESC_F_WRITE)); > >> @@ -1696,10 +1700,6 @@ static inline int virtqueue_add_packed(struct vring_virtqueue *vq, > >> else > >> desc[i].flags = flags; > >> > >> - desc[i].addr = cpu_to_le64(addr); > >> - desc[i].len = cpu_to_le32(len); > >> - desc[i].id = cpu_to_le16(id); > >> - > >> if (unlikely(vq->use_map_api)) { > >> vq->packed.desc_extra[curr].addr = premapped ? > >> DMA_MAPPING_ERROR : addr; > > > >These flags are updated before the flags of the head descriptor at the > >end of the function, at "vq->packed.vring.desc[head].flags = > >head_flags", so the device should not see these. Because of that, the > >relative order between the rest of the fields of the same descriptor > >or other descriptors' fields, except for the head descriptor's flags, > >should not matter. There is a write memory barrier just before > >updating the head's flags. > > > >Also, I don't get why the cache line matters here. Can you expand? Am > >I missing something? >