From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E69063F1AC6 for ; Tue, 4 Aug 2026 02:56:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785812189; cv=none; b=Ckd8VvNZA+sPioeNWoIWeAgQhRYoGZLDXXemKIfxnaMwDmH2T5JHuwmC3tH3iUqQlZxGOG9Ieemh6FiisDbCLWikzM+fM4TPAxDgYLb05uAolxdyI7+JZmyjMFxU2qgm1BbQ9oUfSS9NM3ziUjqevI5fB8gpegtVaXu412rFoNg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785812189; c=relaxed/simple; bh=zqrmGtNYwoSSlb1WHgT3h/cWLH0pWZUE4OokQlWtRek=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hC0/wOHI/Bfx6ZPqFOLyQoXdoWsvM1iLMBucS61MSjrnpNZz/Gn1XKCfqNJLCQGnKJxjVNLI8CT51/34fJHZ7GX65aMpbDscvk2EnhXHEJ+KFh9+Cryf28ALCWYNc4F32ScNpU4pV3s2gi33HZLUQbOHaUJ+2/9ojkhj6W0pZlc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=L7yQpDaG; arc=none smtp.client-ip=209.85.216.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="L7yQpDaG" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-38a0c7e841fso4974765a91.2 for ; Mon, 03 Aug 2026 19:56:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785812187; x=1786416987; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=z1yxnvuZRVlxtf+BJKMZxwYjNsgm5M4Tjb/ynOLuWlk=; b=L7yQpDaGnaM95Z2wqzD1DbzPkNt69swtGsm/6n3ch9JPzF4qqe/UsgMJMQbnqpc1nh hcGYUyZgk4YbVzghGlIxg00VGx+48vPWp/+c03ZasT46QFT4pRmEaE4jnPz9qAAo71sE Dm/IoJDix2qqSamn70MXZUGU4RngOT2io3oV8CEnRgq+STsMjsuTAQIVIBre/GjZQOhb 8lOZOx15CXt9tAby49Ug3r2x5xahMU8/0upRBaQVTdmbDSyxcYBhY3iPfYYFgkcI/6Kz hkHCC3CveF22HtuYV/3qaLgKrDe0fdEG91c32L/npGBP97aipW/Ok3j7CrX/Q+FyD2lQ SOcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785812187; x=1786416987; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=z1yxnvuZRVlxtf+BJKMZxwYjNsgm5M4Tjb/ynOLuWlk=; b=n/5d35vUkwUHeESPYSmFKiwsw2YfNJQzQRqT5PbPX+LJcipHxnuZ3Pw3c96lTWkAbI n2j582T1bCLENc1CJKDavIG/OsRUOnafPjU0RIYQwuJOnc8t5RHS0w+gDfTWterw0E/l T/CMZK3fBUwpDdF0GnmlbWyV25pjAL8IulFV5WHJDT3eRz5wcOYiOjWtFoQnGT8sYZqQ TdtfV8Ht2qoWJZkjzB8M/EORyYovXXO3IaND0oAce4to3kRp9WzTYB6WITgiS9ZM/44U VVFtFlOTT9q6qmstayv9Bn2xh6vXZKhmjFTSRDWZDdjtpVnDhpTie4uzKoKhOtwnSvRM QqhQ== X-Gm-Message-State: AOJu0YyG88rp8cHEOBmYQvKJEVCpMmWJnsedQrAoRQ2QC+wiYYvMDzUS BNtdPZUgE6WiToLjNMm07FBkl2M0vpa/5TjFKSrPIAEiRrt9GHtzNIfU9cwPTgPFJO0I0WV6 X-Gm-Gg: AR+sD102NxqZQAkFsng4AHkRK+5/6muhg/fvGLIOYZFIaT/h6UNyai5JqvXRjBw5zXY CI7g+zWweGNK+yALWyLWLiYjVY3CBF1QO0wGw3Hzb6CMZmJUNU6jqG4NXcOOJ/Y54HPIwyQ/+7V umHtbAzS+KlbnbeMqhjYOtWH+/b02QI68SrrZ8s1pT68fi7+d/bdHq1YEecAeJBKMk5sVJ6TDM9 MWerNNeYSoLcsWhHwxHi2A7JWmxThjMQ8qBfYggUNcj4gvoOvRFqVvPxBRf/ClyZY++Ld64vmQn yxhc2fLpyG+6Gn58AfxoSRHsdF1mNs6zFYsIvWMItPIBI5Gow5AnlFNjb0smseAOH02/auuRvTx XkJMICi6YisMkUYr0eohgEQ9844dexPM84x4cxTFRqIupMwFSG97ay+i40EY+XuijGNgEvNlfA6 mnqmQUh1KAFFkuCDV3i9ocQ6K77T/TTnsaZVqPVsNuG7jFLQUEpCFsdjcrsNvALyfLRLQQwOuBx AmbQRqONJxXtzlQDiba9W9h4dQ= X-Received: by 2002:a17:90b:590c:b0:387:e0db:3fb1 with SMTP id 98e67ed59e1d1-38fbc59b1aamr11609901a91.42.1785812187080; Mon, 03 Aug 2026 19:56:27 -0700 (PDT) Received: from JUNVYYANG-MC1.tencent.com ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2d04ae19ef2sm45373175ad.4.2026.08.03.19.56.24 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 03 Aug 2026 19:56:26 -0700 (PDT) From: Jun Yang To: netdev@vger.kernel.org Cc: Jun Yang , stable@kernel.org, TencentOS Corvus AI , "Michael S. Tsirkin" , Jason Wang , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , kvm@vger.kernel.org, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2] vhost: reset the vring metadata cache on vring reconfiguration Date: Tue, 4 Aug 2026 10:55:29 +0800 Message-ID: <20260804025621.16580-1-juny24602@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260803014823.68623-1-juny24602@gmail.com> References: <20260803014823.68623-1-juny24602@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Jun Yang vq->meta_iotlb[] caches the vhost_iotlb_map that backs each vring metadata region, and iotlb_access_ok() returns early on a cache hit, taking the hit as proof that the region has already been validated: if (vhost_vq_meta_fetch(vq, addr, len, type)) return true; The cache is reset on VHOST_IOTLB_UPDATE and VHOST_IOTLB_INVALIDATE, on device IOTLB (re)initialisation and on vq reset, but not when VHOST_SET_VRING_ADDR replaces vq->desc, vq->avail and vq->used, nor when VHOST_SET_VRING_NUM changes the region sizes. With a device IOTLB attached both ioctls are accepted while the vq is live, and neither validates the addresses at ioctl time: vq_access_ok() and vq_log_used_access_ok() return true early because the addresses are GIOVAs, deferring validation to prefetch time. Once the cache has been populated that deferred validation no longer runs -- vq_meta_prefetch() hits the stale entry and returns true -- and vhost_vq_meta_fetch() keeps translating through the old mapping as map->addr + addr - map->start for an address the mapping no longer covers. vhost_copy_to_user() and vhost_copy_from_user() consume the result with __copy_to_user() and __copy_from_user(), which do not check it either, so a subsequent used ring update or descriptor fetch accesses memory outside the region the IOTLB actually maps. Reset the metadata cache whenever the vring is reconfigured, so the new addresses are pushed back through iotlb_access_ok()'s slow path. Fixes: f88949138058 ("vhost: introduce O(1) vq metadata cache") Cc: stable@kernel.org Reported-by: TencentOS Corvus AI Assisted-by: tencentos-corvus-ai:kimi-k3 Signed-off-by: Jun Yang Acked-by: Michael S. Tsirkin --- v2: add Assisted-by and Acked-by tags drivers/vhost/vhost.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 4c525b3e16ea..77c96993100b 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2126,6 +2126,14 @@ static long vhost_vring_set_num_addr(struct vhost_dev *d, BUG(); } + /* + * The metadata cache holds the IOTLB mapping that backed the previous + * desc/avail/used addresses and vring size, both of which are being + * replaced here. iotlb_access_ok() takes a cache hit as proof that the + * region was validated, so the stale entries have to go. + */ + __vhost_vq_meta_reset(vq); + mutex_unlock(&vq->mutex); return r; -- 2.50.1 (Apple Git-155)