From nobody Mon Feb 9 20:32:46 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1630601468; cv=none; d=zohomail.com; s=zohoarc; b=bP0dTXT3I+dqpUzlupboWL5csKr+rj0BNlVGa3VZjpNJ0oMIXkyseDkxXf4P+U29whDHFpjsq8ScvpuJ80tMQx8Ec+8nKdYBu1CUj0JLi2vf4Beti8SDPiGk1y+HkWbsQWVwhhrmp4PIirpJC/4ShZ+MhNcQ7iJ9BkESJfRA4kw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630601468; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=hnQjUJf863/+mR0eVjMS2XLf+nf0liaJ3GnxfaQW96o=; b=HIlaGe6tOTewCchMZZV4V1pNVP0b0Qn2nwUfshWCY7BMWwyApvSMVe6BXQdu6cKjz+fwuKQZ06it5UX7AITYIMDcgCbLaNjIlU7etF+Bm4OXa4vUnvyvbwcGh73aks7u/2o2jB1g7MgnCgj+Dw0PlaJS4K5/lAbLplkDmykVTkU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1630601468896262.0174778257117; Thu, 2 Sep 2021 09:51:08 -0700 (PDT) Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-456-cvMU-AtaN4q2e2QOC9xY6g-1; Thu, 02 Sep 2021 12:51:04 -0400 Received: by mail-wr1-f69.google.com with SMTP id d10-20020adffbca000000b00157bc86d94eso751023wrs.20 for ; Thu, 02 Sep 2021 09:51:04 -0700 (PDT) Return-Path: Return-Path: Received: from x1w.. (163.red-83-52-55.dynamicip.rima-tde.net. [83.52.55.163]) by smtp.gmail.com with ESMTPSA id f20sm2031259wmb.32.2021.09.02.09.51.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Sep 2021 09:51:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630601467; 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=hnQjUJf863/+mR0eVjMS2XLf+nf0liaJ3GnxfaQW96o=; b=FH3V1eMNVVvQNkQkFi4DrbiKseKGgKQ2RbICV/b4n3FQwjPdqyJBHkZVzsVsxRww6RJgc7 VmDaQxayFVshdryJ6LfM6HI1uqJrx0evFb2sCh6AO42CMPgpB1j7bA2DjTailwkKPQ0chy lPCgaohh6sdC5s6UQ2/rcg+N21DEd8Q= X-MC-Unique: cvMU-AtaN4q2e2QOC9xY6g-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hnQjUJf863/+mR0eVjMS2XLf+nf0liaJ3GnxfaQW96o=; b=A/Kv84Hh8rvx6LGvdRsDc9gOOjZYw+ZIPfHdYr2xqzkEfwoDZROCvx2odiQjmWbc5f sO6BVol0FWURzkJ2pNZnGvoR0Orly4of2zojcj0g7pkf2XVi3feUkvVJJ1rdCZKJYa6Z xgX6jJltTXOqieKRWpvyULhZe0Kkevkt4YFjbGTwnc01O0QqblFa4jiyqHXL8Qv73nW4 qSeOzcQeZGdZleXJtAoqa+/N2YxtuhvCdz2bYfpJB0mGf1ysTWtLJqmI8YNpDLR7baeI ie97kdu4p1eDwT4Wa5KNNvw+FLTJ5eJ65Wv4C+TrFg2a09Uy48USyw2w17ZvCjqR+KeI AqMw== X-Gm-Message-State: AOAM53316vzG1P3NkzsPjUiWBrd8e2ltsEFLrtJ2Yk1yW4KwlAnyPBo+ qKCvVOCZo+qLUPOWclDJRHPpGPEWD3z3PWS3UbZVTB94QDwVl3eT4I6MZl4DJCEQ0zFQSamLN67 5+HmnzNYxRHA8NA== X-Received: by 2002:a05:600c:2186:: with SMTP id e6mr4061390wme.71.1630601463511; Thu, 02 Sep 2021 09:51:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzv883Bvgb+qGXKSmCFdDdyubVsEgDw9NLGLvTI0wvsT8O4RJLHjIHboVbrto2GgSQTbifupg== X-Received: by 2002:a05:600c:2186:: with SMTP id e6mr4061382wme.71.1630601463349; Thu, 02 Sep 2021 09:51:03 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Jason Wang , Stefan Hajnoczi , Cornelia Huck , Stefano Garzarella , Paolo Bonzini , "Michael S. Tsirkin" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v2 5/5] hw/virtio: Have virtqueue_get_avail_bytes() pass caches arg to callees Date: Thu, 2 Sep 2021 18:50:39 +0200 Message-Id: <20210902165039.432786-6-philmd@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210902165039.432786-1-philmd@redhat.com> References: <20210902165039.432786-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1630601470338100001 Both virtqueue_packed_get_avail_bytes() and virtqueue_split_get_avail_bytes() access the region cache, but their caller also does. Simplify by having virtqueue_get_avail_bytes calling both with RCU lock held, and passing the caches as argument. Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- hw/virtio/virtio.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 7d3bf9091ee..0dbfb53e51b 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -985,28 +985,23 @@ static int virtqueue_split_read_next_desc(VirtIODevic= e *vdev, VRingDesc *desc, return VIRTQUEUE_READ_DESC_MORE; } =20 +/* Called within rcu_read_lock(). */ static void virtqueue_split_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, unsigned int *out_byte= s, - unsigned max_in_bytes, unsigned max_out_bytes) + unsigned max_in_bytes, unsigned max_out_bytes, + VRingMemoryRegionCaches *caches) { VirtIODevice *vdev =3D vq->vdev; unsigned int max, idx; unsigned int total_bufs, in_total, out_total; - VRingMemoryRegionCaches *caches; MemoryRegionCache indirect_desc_cache =3D MEMORY_REGION_CACHE_INVALID; int64_t len =3D 0; int rc; =20 - RCU_READ_LOCK_GUARD(); - idx =3D vq->last_avail_idx; total_bufs =3D in_total =3D out_total =3D 0; =20 max =3D vq->vring.num; - caches =3D vring_get_region_caches(vq); - if (!caches) { - goto err; - } =20 while ((rc =3D virtqueue_num_heads(vq, idx)) > 0) { MemoryRegionCache *desc_cache =3D &caches->desc; @@ -1125,32 +1120,28 @@ static int virtqueue_packed_read_next_desc(VirtQueu= e *vq, return VIRTQUEUE_READ_DESC_MORE; } =20 +/* Called within rcu_read_lock(). */ static void virtqueue_packed_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, unsigned int *out_bytes, unsigned max_in_bytes, - unsigned max_out_bytes) + unsigned max_out_bytes, + VRingMemoryRegionCaches *cach= es) { VirtIODevice *vdev =3D vq->vdev; unsigned int max, idx; unsigned int total_bufs, in_total, out_total; MemoryRegionCache *desc_cache; - VRingMemoryRegionCaches *caches; MemoryRegionCache indirect_desc_cache =3D MEMORY_REGION_CACHE_INVALID; int64_t len =3D 0; VRingPackedDesc desc; bool wrap_counter; =20 - RCU_READ_LOCK_GUARD(); idx =3D vq->last_avail_idx; wrap_counter =3D vq->last_avail_wrap_counter; total_bufs =3D in_total =3D out_total =3D 0; =20 max =3D vq->vring.num; - caches =3D vring_get_region_caches(vq); - if (!caches) { - goto err; - } =20 for (;;) { unsigned int num_bufs =3D total_bufs; @@ -1251,6 +1242,8 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigne= d int *in_bytes, uint16_t desc_size; VRingMemoryRegionCaches *caches; =20 + RCU_READ_LOCK_GUARD(); + if (unlikely(!vq->vring.desc)) { goto err; } @@ -1269,10 +1262,12 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsig= ned int *in_bytes, =20 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { virtqueue_packed_get_avail_bytes(vq, in_bytes, out_bytes, - max_in_bytes, max_out_bytes); + max_in_bytes, max_out_bytes, + caches); } else { virtqueue_split_get_avail_bytes(vq, in_bytes, out_bytes, - max_in_bytes, max_out_bytes); + max_in_bytes, max_out_bytes, + caches); } =20 return; --=20 2.31.1