All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC] vhost: set dirty log bits without pinning pages on x86
@ 2026-11-12 10:53 Michael S. Tsirkin
  2026-11-13  1:29 ` zhaoguohan
  2026-11-13 10:54 ` sashiko-bot
  0 siblings, 2 replies; 6+ messages in thread
From: Michael S. Tsirkin @ 2026-11-12 10:53 UTC (permalink / raw)
  To: linux-kernel
  Cc: Khan Zhao, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Jason Wang, Eugenio Pérez,
	kvm, virtualization, netdev

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 <zhaoguohan@kylinos.cn>
Link: https://lore.kernel.org/all/20261112085631.128819-1-zhaoguohan@kylinos.cn/
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---


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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH RFC] vhost: set dirty log bits without pinning pages on x86
  2026-11-12 10:53 [PATCH RFC] vhost: set dirty log bits without pinning pages on x86 Michael S. Tsirkin
@ 2026-11-13  1:29 ` zhaoguohan
  2026-11-13  5:47   ` Michael S. Tsirkin
                     ` (2 more replies)
  2026-11-13 10:54 ` sashiko-bot
  1 sibling, 3 replies; 6+ messages in thread
From: zhaoguohan @ 2026-11-13  1:29 UTC (permalink / raw)
  To: mst
  Cc: bp, dave.hansen, eperezma, hpa, jasowang, kvm, linux-kernel,
	mingo, netdev, tglx, virtualization, x86, zhaoguohan

On Thu, Nov 12, 2026 at 05:53:47AM -0500, Michael S. Tsirkin wrote:
> 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.
[...]
> lightly tested. pls test and resubmit with your tool.

Thanks, this is much simpler than my version. Two comments on
set_bit_user() before I resubmit:

> +	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))		\

errout is only written on the fixup path, so it needs "+r", as in
__try_cmpxchg_user_asm(). With "=r" gcc drops the initialisation; here
(gcc 13.3, x86_64_defconfig) it even shares a register with mask:

     1a3:	lock or %dl,(%rax)
     1a9:	test   %edx,%edx
     1ab:	jne    1bd <log_write+0x5d>
     ...
     1bd:	mov    $0xfffffff2,%eax

so every call returns -EFAULT, and log_write() stops after the first
page.

> +	__uaccess_begin();						\

No access_ok() here. vhost checks the log range when it is set up and
I couldn't find a way around that, but GUP used to reject kernel
addresses by itself. user_access_begin() would keep the check (and the
nospec barrier) local. Something like this, build-tested on x86_64 and
i386 only:

diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -597,19 +597,23 @@ _label:									\
 /* 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;						\
+	u8 __user *__p = (addr);					\
+	int __err = -EFAULT;						\
+	if (user_access_begin(__p, sizeof(*__p))) {			\
+		__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");			\
+		user_access_end();					\
+	}								\
+	__err;								\
 })
 
 /*

If this looks right, I'll fold it in, run my test in a VM and resubmit
with the test as a second patch.

Thanks,
Khan

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH RFC] vhost: set dirty log bits without pinning pages on x86
  2026-11-13  1:29 ` zhaoguohan
@ 2026-11-13  5:47   ` Michael S. Tsirkin
  2026-11-13  6:09   ` Michael S. Tsirkin
  2026-11-13 13:51   ` Michael S. Tsirkin
  2 siblings, 0 replies; 6+ messages in thread
From: Michael S. Tsirkin @ 2026-11-13  5:47 UTC (permalink / raw)
  To: zhaoguohan
  Cc: bp, dave.hansen, eperezma, hpa, jasowang, kvm, linux-kernel,
	mingo, netdev, tglx, virtualization, x86

On Fri, Nov 13, 2026 at 09:29:37AM +0800, zhaoguohan@kylinos.cn wrote:
> On Thu, Nov 12, 2026 at 05:53:47AM -0500, Michael S. Tsirkin wrote:
> > 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.
> [...]
> > lightly tested. pls test and resubmit with your tool.
> 
> Thanks, this is much simpler than my version. Two comments on
> set_bit_user() before I resubmit:
> 
> > +	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))		\
> 
> errout is only written on the fixup path, so it needs "+r", as in
> __try_cmpxchg_user_asm(). With "=r" gcc drops the initialisation; here
> (gcc 13.3, x86_64_defconfig) it even shares a register with mask:
> 
>      1a3:	lock or %dl,(%rax)
>      1a9:	test   %edx,%edx
>      1ab:	jne    1bd <log_write+0x5d>
>      ...
>      1bd:	mov    $0xfffffff2,%eax
> 
> so every call returns -EFAULT, and log_write() stops after the first
> page.
> 
> > +	__uaccess_begin();						\
> 
> No access_ok() here. vhost checks the log range when it is set up

exactly

> and
> I couldn't find a way around that, but GUP used to reject kernel
> addresses by itself. user_access_begin() would keep the check (and the
> nospec barrier) local.

What we *definitely* don't want is nospec on fast path.

> Something like this, build-tested on x86_64 and
> i386 only:
> 
> diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
> --- a/arch/x86/include/asm/uaccess.h
> +++ b/arch/x86/include/asm/uaccess.h
> @@ -597,19 +597,23 @@ _label:									\
>  /* 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;						\
> +	u8 __user *__p = (addr);					\
> +	int __err = -EFAULT;						\
> +	if (user_access_begin(__p, sizeof(*__p))) {			\
> +		__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");			\
> +		user_access_end();					\
> +	}								\
> +	__err;								\
>  })
>  
>  /*
> 
> If this looks right, I'll fold it in, run my test in a VM and resubmit
> with the test as a second patch.
> 
> Thanks,
> Khan


Given the point is to optimize this code, let's optimize.
vhost pre-validates addresses so accesses are safe without
re-checking later.


-- 
MST


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH RFC] vhost: set dirty log bits without pinning pages on x86
  2026-11-13  1:29 ` zhaoguohan
  2026-11-13  5:47   ` Michael S. Tsirkin
@ 2026-11-13  6:09   ` Michael S. Tsirkin
  2026-11-13 13:51   ` Michael S. Tsirkin
  2 siblings, 0 replies; 6+ messages in thread
From: Michael S. Tsirkin @ 2026-11-13  6:09 UTC (permalink / raw)
  To: zhaoguohan
  Cc: bp, dave.hansen, eperezma, hpa, jasowang, kvm, linux-kernel,
	mingo, netdev, tglx, virtualization, x86

On Fri, Nov 13, 2026 at 09:29:37AM +0800, zhaoguohan@kylinos.cn wrote:
> On Thu, Nov 12, 2026 at 05:53:47AM -0500, Michael S. Tsirkin wrote:
> > 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.
> [...]
> > lightly tested. pls test and resubmit with your tool.
> 
> Thanks, this is much simpler than my version. Two comments on
> set_bit_user() before I resubmit:
> 
> > +	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))		\
> 
> errout is only written on the fixup path, so it needs "+r", as in
> __try_cmpxchg_user_asm(). With "=r" gcc drops the initialisation; here
> (gcc 13.3, x86_64_defconfig) it even shares a register with mask:

ah, subtle

> 
>      1a3:	lock or %dl,(%rax)
>      1a9:	test   %edx,%edx
>      1ab:	jne    1bd <log_write+0x5d>
>      ...
>      1bd:	mov    $0xfffffff2,%eax
> 
> so every call returns -EFAULT, and log_write() stops after the first
> page.
> 
> > +	__uaccess_begin();						\
> 
> No access_ok() here. vhost checks the log range when it is set up and
> I couldn't find a way around that, but GUP used to reject kernel
> addresses by itself.

just a side effect of the api, unintentional.

> user_access_begin() would keep the check (and the
> nospec barrier) local.

To expand on why is that not needed, but feel free to dispute
- addresses all pre-validated
- there is no possible gadget here, it's a write, writes are not
  speculated
- lock prefix so no speculation anyway

> Something like this, build-tested on x86_64 and
> i386 only:
> 
> diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
> --- a/arch/x86/include/asm/uaccess.h
> +++ b/arch/x86/include/asm/uaccess.h
> @@ -597,19 +597,23 @@ _label:									\
>  /* 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;						\
> +	u8 __user *__p = (addr);					\
> +	int __err = -EFAULT;						\
> +	if (user_access_begin(__p, sizeof(*__p))) {			\
> +		__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");			\
> +		user_access_end();					\
> +	}								\
> +	__err;								\
>  })
>  
>  /*
> 
> If this looks right, I'll fold it in, run my test in a VM and resubmit
> with the test as a second patch.
> 
> Thanks,
> Khan


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH RFC] vhost: set dirty log bits without pinning pages on x86
  2026-11-12 10:53 [PATCH RFC] vhost: set dirty log bits without pinning pages on x86 Michael S. Tsirkin
  2026-11-13  1:29 ` zhaoguohan
@ 2026-11-13 10:54 ` sashiko-bot
  1 sibling, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-13 10:54 UTC (permalink / raw)
  To: Michael S. Tsirkin; +Cc: kvm, virtualization, Eugenio Perez

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Critical] x86/uaccess, vhost: missing access_ok() check in
  set_bit_user()
- [High] x86/uaccess: set_bit_user() uses write-only =r constraint on
  __err
--

commit 508047a5899f1ab9b68b7a3a4c36fffabf6ab858
Author: Michael S. Tsirkin <mst@redhat.com>

vhost: set dirty log bits without pinning pages on x86

This commit introduces a set_bit_user() helper on x86 to atomically set a
bit in user memory using an exception table entry, and uses it in vhost's
set_bit_to_user() to avoid pinning user pages on the dirty-logging path.

Link: https://lore.kernel.org/all/20261112085631.128819-1-zhaoguohan@kylinos.cn/

> 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;						\
> +})

[Severity: Critical]
Does set_bit_user() need an access_ok() check (or user_access_begin() /
scoped_user_rw_access()) before calling __uaccess_begin()?

Previously, set_bit_to_user() called pin_user_pages_fast(), which rejected
kernel addresses.  Not every vhost path validates vq->log_base with
access_ok() before reaching set_bit_to_user(): when vq->iotlb is non-NULL
and vq->log_used is set (with VHOST_F_LOG_ALL unset), vq_log_access_ok()
skips log_access_ok() because vq_log_used_access_ok() returns true early:

drivers/vhost/vhost.c:vq_log_used_access_ok() {
	if (vq->iotlb)
		return true;

	return !log_used || log_access_ok(log_base, log_addr,
					  vhost_get_used_size(vq, vq->num));
}

When log_used() then runs, it calls log_write_hva() ->
log_write(vq->log_base, ...) -> set_bit_to_user(), allowing userspace to
set bits in arbitrary kernel memory with SMAP disabled.

[Severity: High]
Should the [errout] constraint on __err be "+r" instead of "=r"?

%[errout] is only written by the exception handler when instruction 1b
faults.  With a write-only "=r" constraint, the compiler treats the
initial "__err = 0" assignment as dead and can reuse a register holding a
non-zero input (such as [mask]), causing set_bit_user() to return -EFAULT
even when the atomic orb succeeds and aborting vhost dirty-page logging.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/af7f05bf637337d35fdd10f3e0dbb5e39d7780e7.1794480795.git.mst@redhat.com?part=1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH RFC] vhost: set dirty log bits without pinning pages on x86
  2026-11-13  1:29 ` zhaoguohan
  2026-11-13  5:47   ` Michael S. Tsirkin
  2026-11-13  6:09   ` Michael S. Tsirkin
@ 2026-11-13 13:51   ` Michael S. Tsirkin
  2 siblings, 0 replies; 6+ messages in thread
From: Michael S. Tsirkin @ 2026-11-13 13:51 UTC (permalink / raw)
  To: zhaoguohan
  Cc: bp, dave.hansen, eperezma, hpa, jasowang, kvm, linux-kernel,
	mingo, netdev, tglx, virtualization, x86

On Fri, Nov 13, 2026 at 09:29:37AM +0800, zhaoguohan@kylinos.cn wrote:
> On Thu, Nov 12, 2026 at 05:53:47AM -0500, Michael S. Tsirkin wrote:
> > 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.
> [...]
> > lightly tested. pls test and resubmit with your tool.
> 
> Thanks, this is much simpler than my version. Two comments on
> set_bit_user() before I resubmit:
> 
> > +	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))		\
> 
> errout is only written on the fixup path, so it needs "+r", as in
> __try_cmpxchg_user_asm(). With "=r" gcc drops the initialisation; here
> (gcc 13.3, x86_64_defconfig) it even shares a register with mask:
> 
>      1a3:	lock or %dl,(%rax)
>      1a9:	test   %edx,%edx
>      1ab:	jne    1bd <log_write+0x5d>
>      ...
>      1bd:	mov    $0xfffffff2,%eax
> 
> so every call returns -EFAULT, and log_write() stops after the first
> page.
> 
> > +	__uaccess_begin();						\
> 
> No access_ok() here. vhost checks the log range when it is set up and
> I couldn't find a way around that, but GUP used to reject kernel
> addresses by itself. user_access_begin() would keep the check (and the
> nospec barrier) local. Something like this, build-tested on x86_64 and
> i386 only:
> 
> diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
> --- a/arch/x86/include/asm/uaccess.h
> +++ b/arch/x86/include/asm/uaccess.h
> @@ -597,19 +597,23 @@ _label:									\
>  /* 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;						\
> +	u8 __user *__p = (addr);					\
> +	int __err = -EFAULT;						\
> +	if (user_access_begin(__p, sizeof(*__p))) {			\
> +		__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");			\
> +		user_access_end();					\
> +	}								\
> +	__err;								\
>  })
>  
>  /*
> 
> If this looks right, I'll fold it in, run my test in a VM and resubmit
> with the test as a second patch.
> 
> Thanks,
> Khan


So, you were right, it's better to check access. But I don't like
paying nospec. Let's use the modern scoped accessors?
Like this maybe? Very lightly tested, no time today, sorry.
If you want to use it:

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>



diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index 3a0dd3c2b233e..0bffa43e02179 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -594,6 +594,28 @@ _label:									\
 	__ret;								\
 							})
 
+/* set_bit() on a byte of user memory; returns -EFAULT on fault. */
+#define set_bit_user(nr, addr)					\
+({									\
+	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.


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-13 13:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 10:53 [PATCH RFC] vhost: set dirty log bits without pinning pages on x86 Michael S. Tsirkin
2026-11-13  1:29 ` zhaoguohan
2026-11-13  5:47   ` Michael S. Tsirkin
2026-11-13  6:09   ` Michael S. Tsirkin
2026-11-13 13:51   ` Michael S. Tsirkin
2026-11-13 10:54 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.