From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (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 F06CA42983E for ; Tue, 28 Apr 2026 15:08:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777388896; cv=none; b=p4l8YcXh1445aplkB8LDZ5tctkRaHqRWYOjxyexG0w63bTAEDx55cLR9SxLMrgyf6gVS9WLjO2/urw9+M4Cg5XDpWfErYTq5JNyShjjAhlLLzyIbA0+JZqDWAcoxxywCWgqeuFNtbwFXK9XO8CGQzIoDeN/WSVVv0VAvfJ2OA/0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777388896; c=relaxed/simple; bh=MWBv1DHfHJ5MORln2uC6zteVz62qSKn6Mmg1BPVPwwk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k8mGv1GzYVyOdL1RhkCTu0Qxb6HlRlAqSy5kQT3ZOiqlU3AbVlecr1XnivsJoCxjl9KNnoSorrgs8R1fH0KdTe7hV8BWcvOAeG/QOXRoO6mHChIKpEr/JPuFopUT2X/AiX2HPWzvJNptGZRY36Uk5gAAVWt17ZoLMtqE+I7hnqc= 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=YMsPIICU; arc=none smtp.client-ip=209.85.216.41 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="YMsPIICU" Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-3591cc98871so5327348a91.3 for ; Tue, 28 Apr 2026 08:08:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777388893; x=1777993693; 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; bh=r0D2OjrENWJxNTLidObtJYsE1ofQWJtzpBHNBO0DSMM=; b=YMsPIICU32dADgBVJdBzluZTSM8h3YwmFd/ACYDGvzuo9ElLlMLe1N3L52FbFPneqS nabf7UFky7/uFQZvNvKGuqw/ZE+5PF50wxLLWUbBm2xFyMCM4tCLkcYFEPu3CRLQQOHM DgcAArnIFfAEGiMIjgfUW2FoVurv+19faIWVgjb7RbmZgbhRWx8h+H/ogBfvJ2ZDpAIx R6JLkkH/hU6aFgjMpr/Kq3xC1/EfG52SGS6HetwdlUvOj8Vtn3bnZfNJPMW8jqi2qnAs 7Qskav4tFoNTZBONw1bglLbuQfZ1VRfP8wJC2I+yzNuR4ghmedd+Hv+ajhcfDEl/rEmX sR5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777388893; x=1777993693; 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; bh=r0D2OjrENWJxNTLidObtJYsE1ofQWJtzpBHNBO0DSMM=; b=p2+kHJ0KIuuHOQ4lUCMFbaVhJAUBhTe5fR+7pgLEqXVVA62xwkaACoMtqVjCNzV0l6 56Ve3OIo7+OOe10S3lT+lnXCUa5ka94DHRIWR+0S56iVv97xNBExVRsjyX8xvy8ghEiu FYiF41A8/0WbixXl6sSHx5Dz5/e8HT8LvQx0iN4q/IAIWaCWtIoR5QLodO6LuVPU2lqj LMtbILWpc3lxMGQRTrWY7WN5bx5wg1Pp0PKm13R7t8P3FeQbMlDjrj7qk4Px6F2OmpOc naeKKmW1e1A04pLy1C3qC41aXjMFymn+TKKiP7qQ9uWI/3idJFtFZocyJLt8y61HzRVT nsNw== X-Forwarded-Encrypted: i=1; AFNElJ/RXQHvxcUzhwsW3QGwkP9YqxrPrZBfn7NWSOrkBIBblUwklgM1Ksc9+kZiTLkpAGMnPaV/zsCOD7DCw4Y=@vger.kernel.org X-Gm-Message-State: AOJu0Yzkq3k6M8nlZCYZTGN38101N2VUSJl8rZT27UKrNT8g8AOicLo/ ap6uetK8WZ0fp7t8P/hPMtcljBy5VUYK+9LKsQPFqxk1x9ckeD8JTBhr X-Gm-Gg: AeBDiet2LMg/+cTCXijUHCPDJj4tA4IyxQHW4pRjEQ6nvmQUdW2Cv/Xhq0DDG/AZJVd KuIuqO0NFIq6Z8e1x+Pmsyqp4EumhBA1a5oP1SdMG29UwRYgxC39lnb3wMx0GJ/sg1lnbYQ9t83 QGFzf2Pmy9biiYHiCoiKSYaRkmVWXv4tW9GrLsZ5qBdyxQ/XhviIuqpi+5OaUI/mCenCTGl8poT FoL/tFFx5tzqN0dXOlI25vcjszmbbv3Vr4p/bZZVVnTZPEg4JzJF7mXqrUDv+oZt5uLF1F/Aprq Cyf2SdDDkKFrbgQ50u0t9c6X+AoVxD4hLR8IvjP0HC1Jzn+ebSBiC8Ten5xshzargB5VBpJ8e20 yJppsJt7/2yfQkiZPrzw7WMdoScMA8o5ZakRt0Jm5TdgEPAi3FMefUk1+NNOFT06ns3+WOfam8j +LKps+q5SUZ5cVQpgkc5gBXYkh0tdzUCJ8eohmXvbhBXUFsspHg4kZlnzdPAQnMz21JXW+1NEWF SNCn8dTF6++pAZ323mM32ARUo6Iku0dnYQw7A== X-Received: by 2002:a17:90b:3bcd:b0:35f:b5df:449 with SMTP id 98e67ed59e1d1-3649204f819mr3632492a91.21.1777388892918; Tue, 28 Apr 2026 08:08:12 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36490bd66f6sm1606127a91.3.2026.04.28.08.08.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2026 08:08:12 -0700 (PDT) From: Sungho Bae To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Sungho Bae Subject: [RFC PATCH v7 2/4] virtio_ring: export virtqueue_reinit_vring() for noirq restore Date: Wed, 29 Apr 2026 00:07:40 +0900 Message-Id: <20260428150742.23999-3-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260428150742.23999-1-baver.bae@gmail.com> References: <20260428150742.23999-1-baver.bae@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sungho Bae After a device reset in noirq context the existing vrings must be re-initialized without any memory allocation, because GFP_KERNEL is not available. The internal helpers virtqueue_reset_split() and virtqueue_reset_packed() already reset vring indices and descriptor state in place. Add a thin exported wrapper, virtqueue_reinit_vring(), that dispatches to the appropriate helper based on the ring layout. This will be used by a subsequent patch that adds noirq system-sleep PM callbacks for virtio-mmio. Signed-off-by: Sungho Bae --- drivers/virtio/virtio_ring.c | 58 ++++++++++++++++++++++++++++++++++++ include/linux/virtio_ring.h | 3 ++ 2 files changed, 61 insertions(+) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index fbca7ce1c6bf..d3339b820f6b 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -506,6 +506,15 @@ static void virtqueue_init(struct vring_virtqueue *vq, u32 num) vq->event_triggered = false; vq->num_added = 0; + /* + * Keep IN_ORDER state aligned with a freshly initialized/reset queue. + * For packed IN_ORDER, free_head is unused but harmlessly reset. + */ + if (virtqueue_is_in_order(vq)) { + vq->free_head = 0; + vq->batch_last.id = UINT_MAX; + } + #ifdef DEBUG vq->in_use = false; vq->last_add_time_valid = false; @@ -3936,5 +3945,54 @@ void virtqueue_map_sync_single_range_for_device(const struct virtqueue *_vq, } EXPORT_SYMBOL_GPL(virtqueue_map_sync_single_range_for_device); +/** + * virtqueue_reinit_vring - reinitialize vring state without reallocation + * @_vq: the virtqueue + * + * Reset the avail/used indices and descriptor state of an existing + * virtqueue so it can be reused after a device reset. No memory is + * allocated or freed, making this safe for use in noirq context. + * + * Preconditions for callers: + * 1) The vq must be fully quiesced (no concurrent add/get/kick/IRQ callback). + * 2) Transport/device side must already have stopped/reset this queue. + * 3) All in-flight buffers must already be completed or detached. + * + * If called with outstanding descriptors, free-list state can be corrupted: + * num_free is restored to full capacity while desc_extra next-chain/free_head + * may still represent a partially consumed list. + * + * Return: + * 0 on success, or -EBUSY if preconditions are not met. + */ +int virtqueue_reinit_vring(struct virtqueue *_vq) +{ + struct vring_virtqueue *vq = to_vvq(_vq); + unsigned int num = virtqueue_is_packed(vq) ? + vq->packed.vring.num : vq->split.vring.num; + + /* All in-flight descriptors must be completed or detached */ + if (WARN_ON(vq->vq.num_free != num)) + return -EBUSY; + + if (virtqueue_is_packed(vq)) { + virtqueue_reset_packed(vq); + } else { + /* + * Split queue shadow index should match the visible avail + * index when the queue is fully quiesced. + */ + if (WARN_ON(vq->split.avail_idx_shadow != + virtio16_to_cpu(vq->vq.vdev, + vq->split.vring.avail->idx))) + return -EBUSY; + + virtqueue_reset_split(vq); + } + + return 0; +} +EXPORT_SYMBOL_GPL(virtqueue_reinit_vring); + MODULE_DESCRIPTION("Virtio ring implementation"); MODULE_LICENSE("GPL"); diff --git a/include/linux/virtio_ring.h b/include/linux/virtio_ring.h index c97a12c1cda3..8b421fef4fef 100644 --- a/include/linux/virtio_ring.h +++ b/include/linux/virtio_ring.h @@ -118,6 +118,9 @@ void vring_del_virtqueue(struct virtqueue *vq); /* Filter out transport-specific feature bits. */ void vring_transport_features(struct virtio_device *vdev); +/* Reinitialize a virtqueue without reallocation (safe in noirq context) */ +int virtqueue_reinit_vring(struct virtqueue *_vq); + irqreturn_t vring_interrupt(int irq, void *_vq); u32 vring_notification_data(struct virtqueue *_vq); -- 2.43.0