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 2D4A6C433EF for ; Thu, 31 Mar 2022 10:11:28 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5E1EC8420E; Thu, 31 Mar 2022 12:10:57 +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="AfAaIKWZ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D2EE084209; Thu, 31 Mar 2022 12:10:19 +0200 (CEST) Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) (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 DC6FA841F7 for ; Thu, 31 Mar 2022 12:10:06 +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=3_n1FYgYKBikFXHZQQLTTLQJ.HTRZ-GTTYQNXYX.IJSc.IJ@flex--ascull.bounces.google.com Received: by mail-wm1-x34a.google.com with SMTP id z16-20020a05600c0a1000b0038bebbd8548so1211844wmp.3 for ; Thu, 31 Mar 2022 03:10:06 -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=57Ge2ngZsQx5fKr/r2/F3OqO9NUlHzyQHG6+mHTzfew=; b=AfAaIKWZjukNLQp87syZXuMVYElBWXqyNtizA89D/9rKEAzqqelCRuaPz7hP16kjKH 9AWvuH/yVDFAx7Inrtu3EtBeLt/k/5ystyWCEXFsGedrbzs/c774bW+Vb0eFTHEJfWq2 EktABzheXY/2rdoE5fZPCkF7mi7xoALLlWRnfr44VG8CKl1cjXD23cHqDTS0Y9ciMNJU o7ooKJ2C24FHqR60TQ1Sf3V0X1BWFlwttFvxd+zfOOi4AO5z1aqlOsJ9sHFhjXYf2dkp CHyVnoDngd8SOY/uiKLDkpwHI6rWJnPKF0eVAMhSnWc8jI7eqaJUGm5L3TT8SqA74e9t pJMg== 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=57Ge2ngZsQx5fKr/r2/F3OqO9NUlHzyQHG6+mHTzfew=; b=YNimdHdU25fPN29qKl6xJTDhLDLoTc6p4Q0E19dLGIeI4xcTV1LzRbYk3XKHlhX3B3 0ludYRMJMkVa/z9V6yYtwowFtsFp+T2OmoTPoLPs6LgUJfMBmp7nL2BqlsBBQ5mFk/O2 OFLtJHU1bESCnUM7VKfrv3oLyG3EFbhFMiIi3nUd92jLViS6UCtcZeXZFomnoo9Tg/e1 LRu2PPdHllVBBmknAiMGY66zkFORo4GsOECrz4z68/95LO4CiITm97JvKwdbm1IKnRRg +HevqahgxFHlmgV6el0/32N78Ed87o5auwvexxWoalC72b0qf2kXmUMlvK64eHy38YPf JwIg== X-Gm-Message-State: AOAM533O6MAV/PUnNQ1sgsejamOfBmaMiEneh8s6fspLv/M9uf5+L6TO 8bwBZr4oG+ISwqfx+c7LEVCrXOl2+hyOEi+wwAP99/inpc7p4lU3fJxMayEIJ/WdcPygkOzVog9 pQB++zzSKYddH4bVSqTUWGDnSdSyIkPw0qdtnRCcAgLdeYCgyDDUI5CTunOU= X-Google-Smtp-Source: ABdhPJw2ZzaHL8bPMrcAMukAtq6Gm9eO+NeXjRRD2rvNkTADtSGVW2eJmJuYyhtvxZzpEBCC1c7kf4szP2Y= X-Received: from ascull.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1510]) (user=ascull job=sendgmr) by 2002:adf:ba8f:0:b0:1e9:4afb:179b with SMTP id p15-20020adfba8f000000b001e94afb179bmr3613807wrg.57.1648721406338; Thu, 31 Mar 2022 03:10:06 -0700 (PDT) Date: Thu, 31 Mar 2022 10:09:42 +0000 In-Reply-To: <20220331100949.3637425-1-ascull@google.com> Message-Id: <20220331100949.3637425-5-ascull@google.com> Mime-Version: 1.0 References: <20220331100949.3637425-1-ascull@google.com> X-Mailer: git-send-email 2.35.1.1094.g7c7d902a7c-goog Subject: [PATCH 04/11] virtio_ring: Check used descriptors are chain heads From: Andrew Scull To: u-boot@lists.denx.de Cc: sjg@chromium.org, bmeng.cn@gmail.com, adelva@google.com, keirf@google.com, ptosi@google.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 --- 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.1094.g7c7d902a7c-goog