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 2B404C433F5 for ; Mon, 16 May 2022 10:42:59 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 9837484252; Mon, 16 May 2022 12:42:34 +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="PN3OMKTd"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1DCEE84077; Mon, 16 May 2022 12:42:11 +0200 (CEST) Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) (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 3599583A9F for ; Mon, 16 May 2022 12:42:01 +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=3dyqCYgYKBu4QiSkbbWeeWbU.Seck-ReejbYiji.TUdn.TU@flex--ascull.bounces.google.com Received: by mail-wr1-x449.google.com with SMTP id a18-20020adffb92000000b0020cff565b91so507584wrr.4 for ; Mon, 16 May 2022 03:42:01 -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=5j3uIJx43jO27RF25gZrEQRjqEmVPfTwJjCI/iJn2Wk=; b=PN3OMKTdmTXkhokTb2kb0h0+7MkNLDINRaoyrTvVVPCbCFnA911KJfPG+5zsV3aURo HfW/e2rhbXQOlH9zLwhZ47a+qjrGAUdN7qdD0GrvHdU/m7hW9JEelslOA/GfdkUJtLFO t34dkQKMlVh/bB0rcAbReApMN0OYO6w0FVlzD6lrEqxosYwCS2J2kmoWEFwUJ35yy/6i crXcDC632wSl9E1eDdUAsf8ZiPKdP+DaM/y0GHzQ9IeLm/bnaLr18JvJLKgtrnXdMwX8 wh3N0P9e+bCVNmovnkRpW2MjHY3WgKTit5fsPgpw+b8oTHNN4N7P2VxUnTw7nm/uIzWo 9Tzg== 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=5j3uIJx43jO27RF25gZrEQRjqEmVPfTwJjCI/iJn2Wk=; b=YiryL5UL5p6YigZEtgxPszi38zdRh6FigjoBtJaX7LxEOMKEgmtvMRHjKAxOtq1vSU LSlrt8yIuNypW+8/Xy22uSYH3ynvzvxi6EkKXmN/MGH6n4K2MmLzOlaaM2ELPqUd0DIh 3P/YPoggQZkIVtbloxobmAiRpxqF4YVQTV1KTTY0G8SjzY+kg0l5BGrxEETGZVOnSYlJ IXASlyBDCxa0pruXK8O8EDOsywWPnBp97OuXDO+SC3Lwp6NXHD5vJCdyrFTZWIitHs86 9+8vv2sZHoTUaCzCkDyOWXmFflujWWY1m3/obbKrFmKbqlU4z6+T8ZPVwN8+w+Yffpvh fByA== X-Gm-Message-State: AOAM531X1AhnojTYbqSy8wg0/Bchu7jmLupoeceNaoIjLYaH/sYY8eOj zPzXlzu5u97A8Pg98ducoAoWqtDjdVH8DxFvEJNnHAq+O5zSmX0cbqjz5EQoJE3CG/oNCuYIcU7 59KOqwifphSObpVzBtjVi/SlRH2Fekvo0JcLfnYb5x5fRPkaGrxa9kHVA1YM= X-Google-Smtp-Source: ABdhPJxT0Vs0Fmppt6XU0EXNipodQx3BWH33HaidoTgFf1y4RuAgbw5/XnyyBywN+WXDZ49O4EFtl9KygKg= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:a1c:7410:0:b0:393:fbb4:f52c with SMTP id p16-20020a1c7410000000b00393fbb4f52cmr822951wmc.0.1652697719836; Mon, 16 May 2022 03:41:59 -0700 (PDT) Date: Mon, 16 May 2022 10:41:32 +0000 In-Reply-To: <20220516104140.1047229-1-ascull@google.com> Message-Id: <20220516104140.1047229-5-ascull@google.com> Mime-Version: 1.0 References: <20220516104140.1047229-1-ascull@google.com> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog Subject: [PATCH v3 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, bmeng.cn@gmail.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 73671d79da..f71bab7847 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -82,6 +82,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). @@ -144,6 +147,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; @@ -194,6 +200,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.36.0.550.gb090851708-goog