Skip to contentSkip to conversation

vhost: don't use kmap() to log dirty pages

RFC PATCH In review Jason Wang 2 participants 7 messages May 9, 2019 – May 13, 2019

Subsystems (5)
lore
Share & tools
lore

Jason Wang

author
#0 lore raw

RFC PATCH v2 1/1 vhost: don't use kmap() to log dirty pages

Vhost log dirty pages directly to a userspace bitmap through GUP and kmap_atomic() since kernel doesn't have a set_bit_to_user() helper. This will cause issues for the arch that has virtually tagged caches. The way to fix is to keep using userspace virtual address. Fortunately, futex has arch_futex_atomic_op_inuser() which could be used for setting a bit to user.

Note:
- There're archs (few non popular ones) that don't implement futex helper, we can't log dirty pages. We can fix them e.g for non virtually tagged archs implement a kmap fallback on top or simply disable LOG_ALL features of vhost.
- The helper also requires userspace pointer is located at 4-byte boundary, need to check during dirty log setting

Cc Christoph Hellwig <[email protected]>
Cc James Bottomley <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Darren Hart <[email protected]>
Fixes 3a4d5c94e9593 ("vhost_net: a kernel-level virtio server")

---
Changes from V1:
- switch to use arch_futex_atomic_op_inuser()

---
 drivers/vhost/vhost.c | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)
drivers/vhost/vhost.c +17 -18
index 351af88..4e5a004 100644
@@ -31,6 +31,7 @@
 #include <linux/sched/signal.h>
 #include <linux/interval_tree_generic.h>
 #include <linux/nospec.h>
+#include <asm/futex.h>
 
 #include "vhost.h"
 
@@ -1652,6 +1653,10 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 			r = -EFAULT;
 			break;
 		}
+		if (p & 0x3) {
+			r = -EINVAL;
+			break;
+		}
 		for (i = 0; i < d->nvqs; ++i) {
 			struct vhost_virtqueue *vq;
 			void __user *base = (void __user *)(unsigned long)p;
@@ -1692,31 +1697,27 @@ 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/x86/exception-tables.txt.
- */
-static int set_bit_to_user(int nr, void __user *addr)
+static int set_bit_to_user(int nr, u32 __user *addr)
 {
 	unsigned long log = (unsigned long)addr;
 	struct page *page;
-	void *base;
-	int bit = nr + (log % PAGE_SIZE) * 8;
+	u32 old;
 	int r;
 
 	r = get_user_pages_fast(log, 1, 1, &page);
 	if (r < 0)
 		return r;
 	BUG_ON(r != 1);
-	base = kmap_atomic(page);
-	set_bit(bit, base);
-	kunmap_atomic(base);
+
+	r = arch_futex_atomic_op_inuser(FUTEX_OP_ADD, 1 << nr, &old, addr);
+	/* TODO: fallback to kmap() when -ENOSYS? */
+
 	set_page_dirty_lock(page);
 	put_page(page);
-	return 0;
+	return r;
 }
 
-static int log_write(void __user *log_base,
+static int log_write(u32 __user *log_base,
 		     u64 write_address, u64 write_length)
 {
 	u64 write_page = write_address / VHOST_PAGE_SIZE;
@@ -1726,12 +1727,10 @@ static int log_write(void __user *log_base,
 		return 0;
 	write_length += write_address % VHOST_PAGE_SIZE;
 	for (;;) {
-		u64 base = (u64)(unsigned long)log_base;
-		u64 log = base + write_page / 8;
-		int bit = write_page % 8;
-		if ((u64)(unsigned long)log != log)
-			return -EFAULT;
-		r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
+		u32 __user *log = log_base + write_page / 32;
+		int bit = write_page % 32;
+
+		r = set_bit_to_user(bit, log);
 		if (r < 0)
 			return r;
 		if (write_length <= VHOST_PAGE_SIZE)
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 7 lines · Jason Wang

Vhost log dirty pages directly to a userspace bitmap through GUP and kmap_atomic() since kernel doesn't have a set_bit_to_user() helper. This will cause issues for the arch that has virtually tagged caches. The way to fix is to keep using userspace virtual address. Fortunately, futex has arch_futex_atomic_op_inuser() which could be used for setting a bit to user.

Quoted 7 lines

Note:
- There're archs (few non popular ones) that don't implement futex helper, we can't log dirty pages. We can fix them e.g for non virtually tagged archs implement a kmap fallback on top or simply disable LOG_ALL features of vhost.
- The helper also requires userspace pointer is located at 4-byte boundary, need to check during dirty log setting

Why check? Round it down.

Quoted 29 lines
Cc Christoph Hellwig <[email protected]>
Cc James Bottomley <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Darren Hart <[email protected]>
Fixes 3a4d5c94e9593 ("vhost_net: a kernel-level virtio server")

---
Changes from V1:
- switch to use arch_futex_atomic_op_inuser()

---
 drivers/vhost/vhost.c | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)
drivers/vhost/vhost.c +1 -0
index 351af88..4e5a004 100644
@@ -31,6 +31,7 @@
 #include <linux/sched/signal.h>
 #include <linux/interval_tree_generic.h>
 #include <linux/nospec.h>
+#include <asm/futex.h>
 
 #include "vhost.h"
 

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 1662

Quoted 10 lines
drivers/vhost/vhost.c +4 -0
@@ -1652,6 +1653,10 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 			r = -EFAULT;
 			break;
 		}
+		if (p & 0x3) {
+			r = -EINVAL;
+			break;
+		}
 		for (i = 0; i < d->nvqs; ++i) {
 			struct vhost_virtqueue *vq;
 			void __user *base = (void __user *)(unsigned long)p;

That's an ABI change and might break some userspace. I don't think it's necessary: you are changing individual bits anyway.

Quoted 9 lines
drivers/vhost/vhost.c +0 -5
@@ -1692,31 +1697,27 @@ 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/x86/exception-tables.txt.
- */
-static int set_bit_to_user(int nr, void __user *addr)

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 1707

Quoted 10 lines
drivers/vhost/vhost.c +2 -2
+static int set_bit_to_user(int nr, u32 __user *addr)
 {
 	unsigned long log = (unsigned long)addr;
 	struct page *page;
-	void *base;
-	int bit = nr + (log % PAGE_SIZE) * 8;
+	u32 old;
 	int r;
 
 	r = get_user_pages_fast(log, 1, 1, &page);

OK so the trick is that page is pinned so you don't expect arch_futex_atomic_op_inuser below to fail. get_user_pages_fast guarantees page is not going away but does it guarantee PTE won't be invaidated or write protected?

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 1714

Quoted 10 lines
drivers/vhost/vhost.c +4 -3
 	if (r < 0)
 		return r;
 	BUG_ON(r != 1);
-	base = kmap_atomic(page);
-	set_bit(bit, base);
-	kunmap_atomic(base);
+
+	r = arch_futex_atomic_op_inuser(FUTEX_OP_ADD, 1 << nr, &old, addr);
+	/* TODO: fallback to kmap() when -ENOSYS? */
+

Add a comment why this won't fail? Maybe warn on EFAULT?

Also down the road a variant that does not need tricks like this is still nice to have.

Quoted 30 lines
drivers/vhost/vhost.c +6 -8
 	set_page_dirty_lock(page);
 	put_page(page);
-	return 0;
+	return r;
 }
 
-static int log_write(void __user *log_base,
+static int log_write(u32 __user *log_base,
 		     u64 write_address, u64 write_length)
 {
 	u64 write_page = write_address / VHOST_PAGE_SIZE;
@@ -1726,12 +1727,10 @@ static int log_write(void __user *log_base,
 		return 0;
 	write_length += write_address % VHOST_PAGE_SIZE;
 	for (;;) {
-		u64 base = (u64)(unsigned long)log_base;
-		u64 log = base + write_page / 8;
-		int bit = write_page % 8;
-		if ((u64)(unsigned long)log != log)
-			return -EFAULT;
-		r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
+		u32 __user *log = log_base + write_page / 32;
+		int bit = write_page % 32;
+
+		r = set_bit_to_user(bit, log);
 		if (r < 0)
 			return r;
 		if (write_length <= VHOST_PAGE_SIZE)
Signature · 1 line
1.8.3.1
#2 lore raw
In reply to Jason Wang
Quoted 7 lines · Jason Wang

Vhost log dirty pages directly to a userspace bitmap through GUP and kmap_atomic() since kernel doesn't have a set_bit_to_user() helper. This will cause issues for the arch that has virtually tagged caches. The way to fix is to keep using userspace virtual address. Fortunately, futex has arch_futex_atomic_op_inuser() which could be used for setting a bit to user.

Quoted 7 lines

Note:
- There're archs (few non popular ones) that don't implement futex helper, we can't log dirty pages. We can fix them e.g for non virtually tagged archs implement a kmap fallback on top or simply disable LOG_ALL features of vhost.
- The helper also requires userspace pointer is located at 4-byte boundary, need to check during dirty log setting

Why check? Round it down.

Quoted 29 lines
Cc Christoph Hellwig <[email protected]>
Cc James Bottomley <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Darren Hart <[email protected]>
Fixes 3a4d5c94e9593 ("vhost_net: a kernel-level virtio server")

---
Changes from V1:
- switch to use arch_futex_atomic_op_inuser()

---
 drivers/vhost/vhost.c | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)
drivers/vhost/vhost.c +1 -0
index 351af88..4e5a004 100644
@@ -31,6 +31,7 @@
 #include <linux/sched/signal.h>
 #include <linux/interval_tree_generic.h>
 #include <linux/nospec.h>
+#include <asm/futex.h>
 
 #include "vhost.h"
 

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 1662

Quoted 10 lines
drivers/vhost/vhost.c +4 -0
@@ -1652,6 +1653,10 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 			r = -EFAULT;
 			break;
 		}
+		if (p & 0x3) {
+			r = -EINVAL;
+			break;
+		}
 		for (i = 0; i < d->nvqs; ++i) {
 			struct vhost_virtqueue *vq;
 			void __user *base = (void __user *)(unsigned long)p;

That's an ABI change and might break some userspace. I don't think it's necessary: you are changing individual bits anyway.

Quoted 9 lines
drivers/vhost/vhost.c +0 -5
@@ -1692,31 +1697,27 @@ 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/x86/exception-tables.txt.
- */
-static int set_bit_to_user(int nr, void __user *addr)

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 1707

Quoted 10 lines
drivers/vhost/vhost.c +2 -2
+static int set_bit_to_user(int nr, u32 __user *addr)
 {
 	unsigned long log = (unsigned long)addr;
 	struct page *page;
-	void *base;
-	int bit = nr + (log % PAGE_SIZE) * 8;
+	u32 old;
 	int r;
 
 	r = get_user_pages_fast(log, 1, 1, &page);

OK so the trick is that page is pinned so you don't expect arch_futex_atomic_op_inuser below to fail. get_user_pages_fast guarantees page is not going away but does it guarantee PTE won't be invaidated or write protected?

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 1714

Quoted 10 lines
drivers/vhost/vhost.c +4 -3
 	if (r < 0)
 		return r;
 	BUG_ON(r != 1);
-	base = kmap_atomic(page);
-	set_bit(bit, base);
-	kunmap_atomic(base);
+
+	r = arch_futex_atomic_op_inuser(FUTEX_OP_ADD, 1 << nr, &old, addr);
+	/* TODO: fallback to kmap() when -ENOSYS? */
+

Add a comment why this won't fail? Maybe warn on EFAULT?

Also down the road a variant that does not need tricks like this is still nice to have.

Quoted 30 lines
drivers/vhost/vhost.c +6 -8
 	set_page_dirty_lock(page);
 	put_page(page);
-	return 0;
+	return r;
 }
 
-static int log_write(void __user *log_base,
+static int log_write(u32 __user *log_base,
 		     u64 write_address, u64 write_length)
 {
 	u64 write_page = write_address / VHOST_PAGE_SIZE;
@@ -1726,12 +1727,10 @@ static int log_write(void __user *log_base,
 		return 0;
 	write_length += write_address % VHOST_PAGE_SIZE;
 	for (;;) {
-		u64 base = (u64)(unsigned long)log_base;
-		u64 log = base + write_page / 8;
-		int bit = write_page % 8;
-		if ((u64)(unsigned long)log != log)
-			return -EFAULT;
-		r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
+		u32 __user *log = log_base + write_page / 32;
+		int bit = write_page % 32;
+
+		r = set_bit_to_user(bit, log);
 		if (r < 0)
 			return r;
 		if (write_length <= VHOST_PAGE_SIZE)
Signature · 1 line
1.8.3.1
author
#3 lore raw
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

Vhost log dirty pages directly to a userspace bitmap through GUP and kmap_atomic() since kernel doesn't have a set_bit_to_user() helper. This will cause issues for the arch that has virtually tagged caches. The way to fix is to keep using userspace virtual address. Fortunately, futex has arch_futex_atomic_op_inuser() which could be used for setting a bit to user.

Quoted 8 lines
Quoted 7 lines

Note:
- There're archs (few non popular ones) that don't implement futex helper, we can't log dirty pages. We can fix them e.g for non virtually tagged archs implement a kmap fallback on top or simply disable LOG_ALL features of vhost.
- The helper also requires userspace pointer is located at 4-byte boundary, need to check during dirty log setting

Why check? Round it down.

Will do this.

Quoted 29 lines
Quoted 29 lines
Cc Christoph Hellwig <[email protected]>
Cc James Bottomley <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Darren Hart <[email protected]>
Fixes 3a4d5c94e9593 ("vhost_net: a kernel-level virtio server")

---
Changes from V1:
- switch to use arch_futex_atomic_op_inuser()

---
  drivers/vhost/vhost.c | 35 +++++++++++++++++------------------
  1 file changed, 17 insertions(+), 18 deletions(-)
drivers/vhost/vhost.c +1 -0
index 351af88..4e5a004 100644
@@ -31,6 +31,7 @@
  #include <linux/sched/signal.h>
  #include <linux/interval_tree_generic.h>
  #include <linux/nospec.h>
+#include <asm/futex.h>
 
  #include "vhost.h"
 
Quoted 13 lines

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 1662

Quoted 11 lines
drivers/vhost/vhost.c +4 -0
@@ -1652,6 +1653,10 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
  			r = -EFAULT;
  			break;
  		}
+		if (p & 0x3) {
+			r = -EINVAL;
+			break;
+		}
  		for (i = 0; i < d->nvqs; ++i) {
  			struct vhost_virtqueue *vq;
  			void __user *base = (void __user *)(unsigned long)p;

That's an ABI change and might break some userspace. I don't think it's necessary: you are changing individual bits anyway.

Right.

Quoted 19 lines
Quoted 19 lines
drivers/vhost/vhost.c +2 -7
@@ -1692,31 +1697,27 @@ 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/x86/exception-tables.txt.
- */
-static int set_bit_to_user(int nr, void __user *addr)
+static int set_bit_to_user(int nr, u32 __user *addr)
  {
  	unsigned long log = (unsigned long)addr;
  	struct page *page;
-	void *base;
-	int bit = nr + (log % PAGE_SIZE) * 8;
+	u32 old;
  	int r;
 
Quoted 5 lines

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 1707

Quoted 1 line
drivers/vhost/vhost.c +0 -0
  	r = get_user_pages_fast(log, 1, 1, &page);

OK so the trick is that page is pinned so you don't expect arch_futex_atomic_op_inuser below to fail. get_user_pages_fast guarantees page is not going away but does it guarantee PTE won't be invaidated or write protected?

Good point, then I think we probably need to do manual fixup through fixup_user_fault() if arch_futex_atomic_op_in_user() fail.

Quoted 13 lines

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 1714

Quoted 11 lines
drivers/vhost/vhost.c +4 -3
  	if (r < 0)
  		return r;
  	BUG_ON(r != 1);
-	base = kmap_atomic(page);
-	set_bit(bit, base);
-	kunmap_atomic(base);
+
+	r = arch_futex_atomic_op_inuser(FUTEX_OP_ADD, 1 << nr, &old, addr);
+	/* TODO: fallback to kmap() when -ENOSYS? */
+

Add a comment why this won't fail? Maybe warn on EFAULT?

Quoted 2 lines

Also down the road a variant that does not need tricks like this is still nice to have.

Ok. Let me post a V3.

Thanks

Quoted 24 lines
Quoted 24 lines
drivers/vhost/vhost.c +3 -8
  	set_page_dirty_lock(page);
  	put_page(page);
-	return 0;
+	return r;
  }
 
-static int log_write(void __user *log_base,
+static int log_write(u32 __user *log_base,
  		     u64 write_address, u64 write_length)
  {
  	u64 write_page = write_address / VHOST_PAGE_SIZE;
@@ -1726,12 +1727,10 @@ static int log_write(void __user *log_base,
  		return 0;
  	write_length += write_address % VHOST_PAGE_SIZE;
  	for (;;) {
-		u64 base = (u64)(unsigned long)log_base;
-		u64 log = base + write_page / 8;
-		int bit = write_page % 8;
-		if ((u64)(unsigned long)log != log)
-			return -EFAULT;
-		r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
+		u32 __user *log = log_base + write_page / 32;
Quoted 8 lines
Quoted 8 lines
drivers/vhost/vhost.c +3 -0
+		int bit = write_page % 32;
+
+		r = set_bit_to_user(bit, log);
  		if (r < 0)
  			return r;
  		if (write_length <= VHOST_PAGE_SIZE)
Signature · 1 line
1.8.3.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#4 lore raw
In reply to Michael S. Tsirkin
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

Vhost log dirty pages directly to a userspace bitmap through GUP and kmap_atomic() since kernel doesn't have a set_bit_to_user() helper. This will cause issues for the arch that has virtually tagged caches. The way to fix is to keep using userspace virtual address. Fortunately, futex has arch_futex_atomic_op_inuser() which could be used for setting a bit to user.

Quoted 8 lines
Quoted 7 lines

Note:
- There're archs (few non popular ones) that don't implement futex helper, we can't log dirty pages. We can fix them e.g for non virtually tagged archs implement a kmap fallback on top or simply disable LOG_ALL features of vhost.
- The helper also requires userspace pointer is located at 4-byte boundary, need to check during dirty log setting

Why check? Round it down.

Will do this.

Quoted 29 lines
Quoted 29 lines
Cc Christoph Hellwig <[email protected]>
Cc James Bottomley <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Darren Hart <[email protected]>
Fixes 3a4d5c94e9593 ("vhost_net: a kernel-level virtio server")

---
Changes from V1:
- switch to use arch_futex_atomic_op_inuser()

---
  drivers/vhost/vhost.c | 35 +++++++++++++++++------------------
  1 file changed, 17 insertions(+), 18 deletions(-)
drivers/vhost/vhost.c +1 -0
index 351af88..4e5a004 100644
@@ -31,6 +31,7 @@
  #include <linux/sched/signal.h>
  #include <linux/interval_tree_generic.h>
  #include <linux/nospec.h>
+#include <asm/futex.h>
 
  #include "vhost.h"
 
Quoted 13 lines

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 1662

Quoted 11 lines
drivers/vhost/vhost.c +4 -0
@@ -1652,6 +1653,10 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
  			r = -EFAULT;
  			break;
  		}
+		if (p & 0x3) {
+			r = -EINVAL;
+			break;
+		}
  		for (i = 0; i < d->nvqs; ++i) {
  			struct vhost_virtqueue *vq;
  			void __user *base = (void __user *)(unsigned long)p;

That's an ABI change and might break some userspace. I don't think it's necessary: you are changing individual bits anyway.

Right.

Quoted 19 lines
Quoted 19 lines
drivers/vhost/vhost.c +2 -7
@@ -1692,31 +1697,27 @@ 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/x86/exception-tables.txt.
- */
-static int set_bit_to_user(int nr, void __user *addr)
+static int set_bit_to_user(int nr, u32 __user *addr)
  {
  	unsigned long log = (unsigned long)addr;
  	struct page *page;
-	void *base;
-	int bit = nr + (log % PAGE_SIZE) * 8;
+	u32 old;
  	int r;
 
Quoted 5 lines

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 1707

Quoted 1 line
drivers/vhost/vhost.c +0 -0
  	r = get_user_pages_fast(log, 1, 1, &page);

OK so the trick is that page is pinned so you don't expect arch_futex_atomic_op_inuser below to fail. get_user_pages_fast guarantees page is not going away but does it guarantee PTE won't be invaidated or write protected?

Good point, then I think we probably need to do manual fixup through fixup_user_fault() if arch_futex_atomic_op_in_user() fail.

Quoted 13 lines

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 1714

Quoted 11 lines
drivers/vhost/vhost.c +4 -3
  	if (r < 0)
  		return r;
  	BUG_ON(r != 1);
-	base = kmap_atomic(page);
-	set_bit(bit, base);
-	kunmap_atomic(base);
+
+	r = arch_futex_atomic_op_inuser(FUTEX_OP_ADD, 1 << nr, &old, addr);
+	/* TODO: fallback to kmap() when -ENOSYS? */
+

Add a comment why this won't fail? Maybe warn on EFAULT?

Quoted 2 lines

Also down the road a variant that does not need tricks like this is still nice to have.

Ok. Let me post a V3.

Thanks

Quoted 32 lines
Quoted 32 lines
drivers/vhost/vhost.c +6 -8
  	set_page_dirty_lock(page);
  	put_page(page);
-	return 0;
+	return r;
  }
 
-static int log_write(void __user *log_base,
+static int log_write(u32 __user *log_base,
  		     u64 write_address, u64 write_length)
  {
  	u64 write_page = write_address / VHOST_PAGE_SIZE;
@@ -1726,12 +1727,10 @@ static int log_write(void __user *log_base,
  		return 0;
  	write_length += write_address % VHOST_PAGE_SIZE;
  	for (;;) {
-		u64 base = (u64)(unsigned long)log_base;
-		u64 log = base + write_page / 8;
-		int bit = write_page % 8;
-		if ((u64)(unsigned long)log != log)
-			return -EFAULT;
-		r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
+		u32 __user *log = log_base + write_page / 32;
+		int bit = write_page % 32;
+
+		r = set_bit_to_user(bit, log);
  		if (r < 0)
  			return r;
  		if (write_length <= VHOST_PAGE_SIZE)
Signature · 1 line
1.8.3.1
author
#5 lore raw
Quoted 9 lines · Jason Wang
Quoted 6 lines
Quoted 2 lines

        r = get_user_pages_fast(log, 1, 1, &page);

OK so the trick is that page is pinned so you don't expect arch_futex_atomic_op_inuser below to fail.  get_user_pages_fast guarantees page is not going away but does it guarantee PTE won't be invaidated or write protected?

Good point, then I think we probably need to do manual fixup through fixup_user_fault() if arch_futex_atomic_op_in_user() fail.

This looks like a overkill, we don't need to atomic environment here actually. Instead, just keep pagefault enabled should work. So just introduce arch_futex_atomic_op_inuser_inatomic() variant with pagefault disabled there just for futex should be sufficient.

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#6 lore raw
Quoted 10 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 6 lines
Quoted 2 lines

        r = get_user_pages_fast(log, 1, 1, &page);

OK so the trick is that page is pinned so you don't expect arch_futex_atomic_op_inuser below to fail. get_user_pages_fast guarantees page is not going away but does it guarantee PTE won't be invaidated or write protected?

Quoted 6 lines
Quoted 1 line

Good point, then I think we probably need to do manual fixup through fixup_user_fault() if arch_futex_atomic_op_in_user() fail.

This looks like a overkill, we don't need to atomic environment here actually. Instead, just keep pagefault enabled should work. So just introduce arch_futex_atomic_op_inuser_inatomic() variant with pagefault disabled there just for futex should be sufficient.

Thanks

Ok, instead of using tricks, I think we can gracefully fallback to a get_user()/put_user() pair protected by a mutex.

Let me post a non-rfc version for this.

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization