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_user_bit and use it in vhost, to do just that.

Should not be too hard for non-x86 too.

Reported-by: Khan Zhao <[email protected]>
Link: https://lore.kernel.org/all/[email protected]/
Signed-off-by: Michael S. Tsirkin <[email protected]>
---


lightly tested. pls test and resubmit with your tool.

 arch/x86/include/asm/uaccess.h | 18 ++++++++++++++++++
 drivers/vhost/vhost.c          |  7 +++++++
 2 files changed, 25 insertions(+)

diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index 3a0dd3c2b233e..ba1e7c3f83c8e 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -594,6 +594,24 @@ _label:                                                    
                \
        __ret;                                                          \
                                                        })
 
+/* set_bit() on a byte of user memory; returns -EFAULT on fault. */
+#define set_bit_user(nr, addr)                                 \
+({                                                                     \
+       int __err = 0;                                                  \
+       __uaccess_begin();                                              \
+       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" (*(u8 __user *)(addr))                \
+                    : [mask] "q" ((unsigned char)BIT(nr))              \
+                    : "memory", "cc");                         \
+       __uaccess_end();                                                \
+       __err ? -EFAULT : 0;                                            \
+})
+
 /*
  * 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 2f2c45d208832..9ba16cbd788cc 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -2405,6 +2405,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.
@@ -2427,6 +2433,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)
-- 
MST


Reply via email to