From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2E2E2C433F5 for ; Wed, 13 Apr 2022 14:22:29 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B14C583D76; Wed, 13 Apr 2022 16:21:59 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="I29WvnMU"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D05D983DEF; Wed, 13 Apr 2022 16:21:57 +0200 (CEST) Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id A7A7C83DD2 for ; Wed, 13 Apr 2022 16:21:53 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=3gNxWYgYKBq0NfPhYYTbbTYR.PbZh-ObbgYVfgf.QRak.QR@flex--ascull.bounces.google.com Received: by mail-wr1-x44a.google.com with SMTP id p18-20020adfc392000000b00207a2102f9bso420888wrf.7 for ; Wed, 13 Apr 2022 07:21:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=kyD6C16nc7n/91/O4HHyHmWlwu+JfmOfSwZ0vCyaqeg=; b=I29WvnMU8EgdghplNijblHIfdRsKKBFK2Hr5Bu1964NEl+XEkeRE7rVdHEHYGMnBGl 4FbrqPIpVB4TXxujex/ixxSxdIbTd7Nds8WKLuafssMJuPAgdCywUDQp+qz1pLBSMlFI X09W8IWTA6EfWfBM6E5C5TesP/k4ldrZ+TFqx20qjQmp1q3MU9m825Z37lXSRLvBU4QN /8mGqM48Lna3QsKvvZeaEJlShs/Z4uXdWycxxwJHzdotP63ztfO3zAAObp6GizUFrumJ qrUpwfOAgCreclKiG2yBDA0zxl71rvk+grD50Hfk+1jpyy/XauXIuLUReO0/cTp8O4ev c7Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=kyD6C16nc7n/91/O4HHyHmWlwu+JfmOfSwZ0vCyaqeg=; b=7kD/Fi1XKtHJQIjK4h5IZoRBwSeL9bsUrxYmJKsDWrpldgcUkEU0gd5daVr4zop0VI ogL2PaB75A92lcmsbeGUJc7ymoGnlbFxX1VzdmquYExuxbBSZKYDNhI2cuzsJnI+GLph PL14BFaOmPWj+oKCgoOEw+xV4OcmPdEZiWbpst0/2VOqftu/6WgTipJCq0wWY4xMAl5o N8P/PyrNiSrCmDfoybJwtU2PlfafUxIkhe4U1+T3BPRuy0S23/mdr7GZj3rDJVb3S0Dz wEZt0+fx4Uo5/3cI+fW1ppltMTwqWa2oUm9LdecqoNWU5wiAbXUCtAxUs4kh+/gF/+O/ xLMQ== X-Gm-Message-State: AOAM530b4PRPaDwfxjsK8GG3RhRP9eJsX4wLTeJKvdSiF8x3NBLIwkn5 xr4nAIIvabxlO9HN9EIKAr/ZahTTUm6Ozx4wCKFNfnJCN3bsn+LzUw1GSMAUAHxrLeaJT323h1v d0TImQUVtqlwM8Z2ojpvq25R/QkkN6q6VNV3ynXwADa+MucZ1zT6SS+79KFs= X-Google-Smtp-Source: ABdhPJyzuuQSdE1DCxA1fZAPENfBavS0ytHC6zQky/l1NyADVhBNqS5CSUEescrNp1cradP1Z+1li3NXoZ8= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:a1c:35c7:0:b0:38e:4c59:6788 with SMTP id c190-20020a1c35c7000000b0038e4c596788mr21332wma.1.1649859712921; Wed, 13 Apr 2022 07:21:52 -0700 (PDT) Date: Wed, 13 Apr 2022 14:21:29 +0000 In-Reply-To: <20220413142137.560987-1-ascull@google.com> Message-Id: <20220413142137.560987-5-ascull@google.com> Mime-Version: 1.0 References: <20220413142137.560987-1-ascull@google.com> X-Mailer: git-send-email 2.35.1.1178.g4f1659d476-goog Subject: [PATCH v2 04/12] virtio_ring: Check used descriptors are chain heads From: Andrew Scull To: u-boot@lists.denx.de Cc: sjg@chromium.org, trini@konsulko.com, Andrew Scull Content-Type: text/plain; charset="UTF-8" X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean When the device returns used buffers, it should refer to the descriptor that is the head of the descriptor chain for that buffer. Confirm this to be the case by tracking the head of descriptor chains that have been made available to the device. Signed-off-by: Andrew Scull Reviewed-by: Simon Glass --- drivers/virtio/virtio_ring.c | 12 ++++++++++++ include/virtio_ring.h | 2 ++ 2 files changed, 14 insertions(+) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 69fd8c6aa0..383d574cb0 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -84,6 +84,9 @@ int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[], /* Update free pointer */ vq->free_head = i; + /* Mark the descriptor as the head of a chain. */ + vq->vring_desc_shadow[head].chain_head = true; + /* * Put entry in available array (but don't update avail->idx * until they do sync). @@ -146,6 +149,9 @@ static void detach_buf(struct virtqueue *vq, unsigned int head) { unsigned int i; + /* Unmark the descriptor as the head of a chain. */ + vq->vring_desc_shadow[head].chain_head = false; + /* Put back on free list: unmap first-level descriptors and find end */ i = head; @@ -196,6 +202,12 @@ void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len) return NULL; } + if (unlikely(!vq->vring_desc_shadow[i].chain_head)) { + printf("(%s.%d): id %u is not a head\n", + vq->vdev->name, vq->index, i); + return NULL; + } + detach_buf(vq, i); vq->last_used_idx++; /* diff --git a/include/virtio_ring.h b/include/virtio_ring.h index 52cbe77c0a..c77c212cff 100644 --- a/include/virtio_ring.h +++ b/include/virtio_ring.h @@ -61,6 +61,8 @@ struct vring_desc_shadow { u32 len; u16 flags; u16 next; + /* Metadata about the descriptor. */ + bool chain_head; }; struct vring_avail { -- 2.35.1.1178.g4f1659d476-goog