From: Peng Hao <flyingpenghao@gmail.com>
To: mst@redhat.com, jasowangio@gmail.com
Cc: virtualization@lists.linux.dev
Subject: [PATCH] virtio_pci_modern: skip unused trailing driver feature words
Date: Sun, 20 Sep 2026 14:57:46 +0800 [thread overview]
Message-ID: <20260920065746.86415-1-flyingpeng@tencent.com> (raw)
Since commit 69b9461512246 ("virtio_pci_modern: allow configuring
extended features"), vp_modern_set_extended_features() writes all 128
feature bits on every device. Each 32-bit word requires one write to
guest_feature_select and another to guest_feature, even when the upper
64 feature bits are all zero.
Feature negotiation starts after a device reset, which clears the
device-side driver features. There is therefore no need to write
trailing zero 64-bit words on the first feature finalization.
finalize_features() can, however, be called again without an intervening
reset when a driver's validate callback changes the negotiated features.
Remember how many words were written since the last reset and include
that range in the next write, so that a repeated finalization can clear
features which were previously enabled.
This avoids four MMIO writes for devices which negotiate no feature
above bit 63, while preserving the full-overwrite behavior when the
negotiated feature range shrinks.
Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
drivers/virtio/virtio_pci_modern_dev.c | 22 +++++++++++++++++++++-
include/linux/virtio_pci_modern.h | 3 +++
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/drivers/virtio/virtio_pci_modern_dev.c b/drivers/virtio/virtio_pci_modern_dev.c
index 413a8c353463..8211e491a17b 100644
--- a/drivers/virtio/virtio_pci_modern_dev.c
+++ b/drivers/virtio/virtio_pci_modern_dev.c
@@ -230,6 +230,8 @@ int vp_modern_probe(struct virtio_pci_modern_device *mdev)
check_offsets();
+ mdev->driver_features_u64s = 1;
+
if (mdev->device_id_check) {
devid = mdev->device_id_check(pci_dev);
if (devid < 0)
@@ -446,14 +448,29 @@ void vp_modern_set_extended_features(struct virtio_pci_modern_device *mdev,
const u64 *features)
{
struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
+ u8 u64s = VIRTIO_FEATURES_U64S;
+ u8 write_u64s;
int i;
- for (i = 0; i < VIRTIO_FEATURES_BITS / 32; i++) {
+ /*
+ * A reset clears the device-side driver features, so trailing zero
+ * words need not be written. Include words written by the previous
+ * call, though, so a repeated feature finalization can clear features
+ * that were previously enabled.
+ */
+ while (u64s > 1 && !features[u64s - 1])
+ u64s--;
+
+ write_u64s = max(u64s, mdev->driver_features_u64s);
+
+ for (i = 0; i < write_u64s * 2; i++) {
u32 cur = features[i >> 1] >> (32 * (i & 1));
vp_iowrite32(i, &cfg->guest_feature_select);
vp_iowrite32(cur, &cfg->guest_feature);
}
+
+ mdev->driver_features_u64s = u64s;
}
EXPORT_SYMBOL_GPL(vp_modern_set_extended_features);
@@ -495,6 +512,9 @@ void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
{
struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
+ if (!status)
+ mdev->driver_features_u64s = 1;
+
/*
* Per memory-barriers.txt, wmb() is not needed to guarantee
* that the cache coherent memory writes have completed
diff --git a/include/linux/virtio_pci_modern.h b/include/linux/virtio_pci_modern.h
index 9a3f2fc53bd6..8df287d545c9 100644
--- a/include/linux/virtio_pci_modern.h
+++ b/include/linux/virtio_pci_modern.h
@@ -27,6 +27,8 @@
* Returns the found device id or ERRNO
* @dma_mask: Optional mask instead of the traditional DMA_BIT_MASK(64),
* for vendor devices with DMA space address limitations
+ * @driver_features_u64s: Number of 64-bit driver feature words programmed
+ * since the last device reset
*/
struct virtio_pci_modern_device {
struct pci_dev *pci_dev;
@@ -49,6 +51,7 @@ struct virtio_pci_modern_device {
int (*device_id_check)(struct pci_dev *pdev);
u64 dma_mask;
+ u8 driver_features_u64s;
};
/*
--
2.43.7
next reply other threads:[~2026-09-20 6:57 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-20 6:57 Peng Hao [this message]
2026-09-20 7:02 ` [PATCH] virtio_pci_modern: skip unused trailing driver feature words sashiko-bot
2026-09-20 8:40 ` Michael S. Tsirkin
2026-09-24 7:08 ` Hao Peng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260920065746.86415-1-flyingpeng@tencent.com \
--to=flyingpenghao@gmail.com \
--cc=jasowangio@gmail.com \
--cc=mst@redhat.com \
--cc=virtualization@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox