Re: [PATCH 1/2] vhost: set dirty log bits without pinning pages on x86

From: Michael S. Tsirkin

Date: Thu Nov 12 2026 - 05:02:23 EST


On Thu, Nov 12, 2026 at 04:56:30PM +0800, zhaoguohan@xxxxxxxxxx wrote:
> From: Khan Zhao <zhaoguohan@xxxxxxxxxx>
>
> set_bit_to_user() pins the log page with pin_user_pages_fast(), maps it
> with kmap_atomic(), sets one bit and then unpins the page and marks it
> dirty. This runs for every bit set in the dirty log, e.g. on each used
> ring update while logging is enabled for live migration.
>
> log_write() always passes a byte address and a bit number in [0, 7], so
> the update can be done as a byte-sized cmpxchg on the user address. On
> x86, try that first using unsafe_try_cmpxchg_user() inside a
> user_access_begin()/user_access_end() section with page faults disabled.
> If the access faults (page not present, read-only or needing COW), or
> the cmpxchg loses to concurrent updates of the same byte four times in
> a row, fall back to the existing pin/kmap/set_bit path, which can fault
> the page in.
>
> unsafe_try_cmpxchg_user() is only implemented on x86, so other
> architectures keep using the existing path.
>
> On an x86_64 KVM guest (i7-12800HX host), a synthetic used-ring
> workload with logging enabled went from 243.60 to 171.11 ns per
> descriptor, averaged over five alternating runs. This is the
> end-to-end cost per descriptor, not live migration performance.
>
> Signed-off-by: Khan Zhao <zhaoguohan@xxxxxxxxxx>
> ---
> drivers/vhost/vhost.c | 50 ++++++++++++++++++++++++++++++++++++++-----
> 1 file changed, 45 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index e29f154706da..9c3045504709 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -2527,11 +2527,7 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
> }
> EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
>
> -/* 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.
> - */
> -static int set_bit_to_user(int nr, void __user *addr)
> +static int set_bit_to_user_slow(int nr, void __user *addr)
> {
> unsigned long log = (unsigned long)addr;
> struct page *page;
> @@ -2550,6 +2546,50 @@ static int set_bit_to_user(int nr, void __user *addr)
> return 0;
> }
>
> +#ifdef CONFIG_X86
> +static bool set_bit_to_user_fast(int nr, void __user *addr)
> +{
> + u8 __user *p = addr;
> + u8 old;
> + int retries = 4;
> + bool success = false;
> +
> + /*
> + * log_write() passes a byte address and a bit number in [0, 7].
> + * On x86, update just that byte to avoid crossing a page boundary.
> + * Bound retries so a contended byte cannot hold up the worker.
> + */
> + pagefault_disable();
> + if (!user_access_begin(p, sizeof(*p)))
> + goto out;
> + unsafe_get_user(old, p, fault);
> + do {
> + if (unsafe_try_cmpxchg_user(p, &old, old | BIT(nr), fault)) {
> + success = true;
> + break;
> + }
> + } while (--retries);
> +fault:
> + user_access_end();
> +out:
> + pagefault_enable();
> + return success;
> +}
> +#else
> +static bool set_bit_to_user_fast(int nr, void __user *addr)
> +{
> + return false;
> +}
> +#endif /* CONFIG_X86 */
> +
> +static int set_bit_to_user(int nr, void __user *addr)
> +{
> + if (set_bit_to_user_fast(nr, addr))
> + return 0;
> + /* Resolve faults, or use the native bit layout on other architectures. */
> + return set_bit_to_user_slow(nr, addr);
> +}
> +
> static int log_write(void __user *log_base,
> u64 write_address, u64 write_length)
> {


Actually why do we need all the unsafe trickery, is there
a reason? below seems way simpler?

+static int set_bit_to_user(int nr, void __user *addr)
+{
+ u8 __user *p = addr;
+ u8 old;
+ int i, ret;
+
+ if (get_user(old, p))
+ return set_bit_to_user_slow(nr, addr);
+ for (i = 0; i < 4; i++) {
+ ret = __try_cmpxchg_user(p, &old, old | BIT(nr), fault);
+ if (!ret)
+ return 0;
+ }
+ return set_bit_to_user_slow(nr, addr);
+}








> --
> 2.43.0