[PATCH v2 1/2] vhost: set dirty log bits without pinning pages on x86
From: zhaoguohan
Date: Tue Nov 17 2026 - 03:34:05 EST
From: "Michael S. Tsirkin" <mst@xxxxxxxxxx>
vhost had a TODO to replace page pinning with a version
of set_bit that works on userspace memory, since forever.
It is really best to avoid GUP on fast path entirely.
Implement set_bit_user and use it in vhost, to do just that.
Should not be too hard for non-x86 too.
Reported-by: Khan Zhao <zhaoguohan@xxxxxxxxxx>
Link: https://lore.kernel.org/all/20261112085631.128819-1-zhaoguohan@xxxxxxxxxx/
Signed-off-by: Michael S. Tsirkin <mst@xxxxxxxxxx>
[Khan: declare efault with __label__ so it is local to the macro]
Signed-off-by: Khan Zhao <zhaoguohan@xxxxxxxxxx>
---
v2: new in this series; replaces my v1 patch 1. Taken from
Michael's reply in the RFC thread, with __label__ added for efault.
arch/x86/include/asm/uaccess.h | 23 +++++++++++++++++++++++
drivers/vhost/vhost.c | 7 +++++++
2 files changed, 30 insertions(+)
diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index f4700ad6b5ec..a727ef8084f4 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -540,6 +540,29 @@ _label: \
__ret; \
})
+/* set_bit() on a byte of user memory; returns -EFAULT on fault. */
+#define set_bit_user(nr, addr) \
+({ \
+ __label__ efault; \
+ int __err = -EFAULT; \
+ u8 __user *__p = (addr); \
+ scoped_user_rw_access(__p, efault) { \
+ __err = 0; \
+ asm volatile("\n" \
+ "1: " LOCK_PREFIX "orb %[mask], %[ptr]\n" \
+ "2:\n" \
+ _ASM_EXTABLE_TYPE_REG(1b, 2b, \
+ EX_TYPE_EFAULT_REG, \
+ %[errout]) \
+ : [errout] "+r" (__err), \
+ [ptr] "+m" (*__p) \
+ : [mask] "q" ((unsigned char)BIT(nr)) \
+ : "memory", "cc"); \
+ } \
+efault: \
+ __err; \
+})
+
/*
* We want the unsafe accessors to always be inlined and use
* the error labels - thus the macro games.
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index e29f154706da..d426f303c107 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -2527,6 +2527,12 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
}
EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
+#ifdef CONFIG_X86
+static int set_bit_to_user(int nr, void __user *addr)
+{
+ return set_bit_user(nr, addr);
+}
+#else
/* TODO: This is really inefficient. We need something like get_user()
* (instruction directly accesses the data, with an exception table entry
* returning -EFAULT). See Documentation/arch/x86/exception-tables.rst.
@@ -2549,6 +2555,7 @@ static int set_bit_to_user(int nr, void __user *addr)
unpin_user_pages_dirty_lock(&page, 1, true);
return 0;
}
+#endif /* CONFIG_X86 */
static int log_write(void __user *log_base,
u64 write_address, u64 write_length)
--
2.43.0