Skip to contentSkip to conversation

um: remove set_fs

RFC PATCH In review Christoph Hellwig 4 participants 12 messages Jul 9, 2021 – Aug 27, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 um: remove set_fs

Remove address space overrides using set_fs() for user mode Linux.

Compile tested only.

---
 arch/um/Kconfig                   |  1 -
 arch/um/include/asm/thread_info.h |  4 ----
 arch/um/include/asm/uaccess.h     | 21 +++++++++++++++++++--
 arch/um/kernel/skas/uaccess.c     | 23 -----------------------
 arch/x86/um/asm/segment.h         |  8 --------
 5 files changed, 19 insertions(+), 38 deletions(-)
arch/um/Kconfig +0 -1
index 57cfd9a1c082..2a87f46259c8 100644
@@ -19,7 +19,6 @@ config UML
 	select GENERIC_IRQ_SHOW
 	select GENERIC_CPU_DEVICES
 	select HAVE_GCC_PLUGINS
-	select SET_FS
 	select TTY # Needed for line.c
 
 config MMU
arch/um/include/asm/thread_info.h +0 -4
index 3b1cb8b3b186..1395cbd7e340 100644
@@ -22,9 +22,6 @@ struct thread_info {
 	__u32			cpu;		/* current CPU */
 	int			preempt_count;  /* 0 => preemptable,
 						   <0 => BUG */
-	mm_segment_t		addr_limit;	/* thread address space:
-					 	   0-0xBFFFFFFF for user
-						   0-0xFFFFFFFF for kernel */
 	struct thread_info	*real_thread;    /* Points to non-IRQ stack */
 	unsigned long aux_fp_regs[FP_SIZE];	/* auxiliary fp_regs to save/restore
 						   them out-of-band */
@@ -36,7 +33,6 @@ struct thread_info {
 	.flags =		0,		\
 	.cpu =		0,			\
 	.preempt_count = INIT_PREEMPT_COUNT,	\
-	.addr_limit =	KERNEL_DS,		\
 	.real_thread = NULL,			\
 }
 
arch/um/include/asm/uaccess.h +19 -2
index fe66d659acad..85e0275c714b 100644
@@ -8,6 +8,7 @@
 #define __UM_UACCESS_H
 
 #include <asm/elf.h>
+#include <asm/unaligned.h>
 
 #define __under_task_size(addr, size) \
 	(((unsigned long) (addr) < TASK_SIZE) && \
@@ -42,8 +43,24 @@ static inline int __access_ok(unsigned long addr, unsigned long size)
 {
 	return __addr_range_nowrap(addr, size) &&
 		(__under_task_size(addr, size) ||
-		__access_ok_vsyscall(addr, size) ||
-		uaccess_kernel());
+		 __access_ok_vsyscall(addr, size));
 }
 
+/* no pagefaults for kernel addresses in um */
+#define HAVE_GET_KERNEL_NOFAULT 1
+
+#define __get_kernel_nofault(dst, src, type, err_label)			\
+do {									\
+	*((type *)dst) = get_unaligned((type *)(src));			\
+	if (0) /* make sure the label looks used to the compiler */	\
+		goto err_label;						\
+} while (0)
+
+#define __put_kernel_nofault(dst, src, type, err_label)			\
+do {									\
+	put_unaligned(*((type *)src), (type *)(dst));			\
+	if (0) /* make sure the label looks used to the compiler */	\
+		goto err_label;						\
+} while (0)
+
 #endif
arch/um/kernel/skas/uaccess.c +0 -23
index 2dec915abe6f..f716ba0e66ee 100644
@@ -145,11 +145,6 @@ static int copy_chunk_from_user(unsigned long from, int len, void *arg)
 
 unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
 {
-	if (uaccess_kernel()) {
-		memcpy(to, (__force void*)from, n);
-		return 0;
-	}
-
 	return buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to);
 }
 EXPORT_SYMBOL(raw_copy_from_user);
@@ -165,11 +160,6 @@ static int copy_chunk_to_user(unsigned long to, int len, void *arg)
 
 unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
 {
-	if (uaccess_kernel()) {
-		memcpy((__force void *) to, from, n);
-		return 0;
-	}
-
 	return buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from);
 }
 EXPORT_SYMBOL(raw_copy_to_user);
@@ -193,11 +183,6 @@ long __strncpy_from_user(char *dst, const char __user *src, long count)
 	long n;
 	char *ptr = dst;
 
-	if (uaccess_kernel()) {
-		strncpy(dst, (__force void *) src, count);
-		return strnlen(dst, count);
-	}
-
 	n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
 		      &ptr);
 	if (n != 0)
@@ -214,11 +199,6 @@ static int clear_chunk(unsigned long addr, int len, void *unused)
 
 unsigned long __clear_user(void __user *mem, unsigned long len)
 {
-	if (uaccess_kernel()) {
-		memset((__force void*)mem, 0, len);
-		return 0;
-	}
-
 	return buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL);
 }
 EXPORT_SYMBOL(__clear_user);
@@ -239,9 +219,6 @@ long __strnlen_user(const void __user *str, long len)
 {
 	int count = 0, n;
 
-	if (uaccess_kernel())
-		return strnlen((__force char*)str, len) + 1;
-
 	n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
 	if (n == 0)
 		return count + 1;
arch/x86/um/asm/segment.h +0 -8
index 453db377150d..2ef507bc6989 100644
@@ -8,12 +8,4 @@ extern int host_gdt_entry_tls_min;
 #define GDT_ENTRY_TLS_MIN host_gdt_entry_tls_min
 #define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
 
-typedef struct {
-	unsigned long seg;
-} mm_segment_t;
-
-#define MAKE_MM_SEG(s)	((mm_segment_t) { (s) })
-#define KERNEL_DS	MAKE_MM_SEG(~0UL)
-#define USER_DS		MAKE_MM_SEG(TASK_SIZE)
-
 #endif
Signature · 7 lines
2.30.2


_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
maintainer
#1 lore raw
Quoted 3 lines · Christoph Hellwig

Remove address space overrides using set_fs() for user mode Linux.

Compile tested only.

Seems to survive my normal (wireless related) tests :)

But I don't really understand what any of it does, so ...

In particular, why no pagefaults for kernel addresses?

johannes

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um

#2 lore raw
Quoted 5 lines · Johannes Berg

Seems to survive my normal (wireless related) tests :)

But I don't really understand what any of it does, so ...

In particular, why no pagefaults for kernel addresses?

No idea. But the existing uaccess_kernel() versions of the user copy routines assume it, so this patch just keeps that behavior when switching to the proper helpers.

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um

#3 lore raw
In reply to Johannes Berg

Richard, Anton - any comments?

Quoted 5 lines · Johannes Berg
Quoted 3 lines · Christoph Hellwig

Remove address space overrides using set_fs() for user mode Linux.

Compile tested only.

Quoted 7 lines

Seems to survive my normal (wireless related) tests :)

But I don't really understand what any of it does, so ...

In particular, why no pagefaults for kernel addresses?

johannes

---end quoted text---

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um

maintainer
#4 lore raw
Quoted 8 lines · Christoph Hellwig

Richard, Anton - any comments?

Quoted 5 lines · Johannes Berg
Quoted 3 lines · Christoph Hellwig

Remove address space overrides using set_fs() for user mode Linux.

Compile tested only.

Quoted 9 lines
Quoted 7 lines

Seems to survive my normal (wireless related) tests :)

But I don't really understand what any of it does, so ...

In particular, why no pagefaults for kernel addresses?

johannes

---end quoted text---

I will have a look at it today. Sorry, snowed under OVS/OVN.

Signature · 8 lines
Anton R. Ivanov
Cambridgegreys Limited. Registered in England. Company Number 10273661
https://www.cambridgegreys.com/

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
maintainer
#5 lore raw
In reply to Christoph Hellwig
Quoted 14 lines · Christoph Hellwig

Richard, Anton - any comments?

Quoted 11 lines · Johannes Berg
Quoted 3 lines · Christoph Hellwig

Remove address space overrides using set_fs() for user mode Linux.

Compile tested only.

Seems to survive my normal (wireless related) tests :)

But I don't really understand what any of it does, so ...

In particular, why no pagefaults for kernel addresses?

Quoted 8 lines
Quoted 1 line

johannes

---end quoted text---

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um

In first read - you do not want to do that.

buffer_op is slow (even with all the tweaks we have done to that). It is one of the reasons UML userspace is slower than it should be.

The primary reason for that is that it never copies more than a page at a time and pages in/out a page at a time.

We retain reasonable kernel speed because we bypass it for kernel - "if (uaccess_kernel())".

Unless I am missing something, this change will use the slow path currently used for userspace for the kernel.

Signature · 8 lines
Anton R. Ivanov
Cambridgegreys Limited. Registered in England. Company Number 10273661
https://www.cambridgegreys.com/

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
#6 lore raw
Quoted 9 lines · Anton Ivanov

In first read - you do not want to do that.

buffer_op is slow (even with all the tweaks we have done to that). It is one of the reasons UML userspace is slower than it should be.

The primary reason for that is that it never copies more than a page at a time and pages in/out a page at a time.

We retain reasonable kernel speed because we bypass it for kernel - "if (uaccess_kernel())".

Unless I am missing something, this change will use the slow path currently used for userspace for the kernel.

uaccess_kernel() is not the fast path, it is the exception path when someone uses the uaccess helper on kernel pointer after doing a set_fs(KERNEL_DS). And this path is entirel gone once CONFIG_SET_FS is not set and replaced with calls to __get_kernel_nofault / __put_kernel_nofault.

In other words - very little generated code actually changes with this patch, except for the context switch path, which gets simplified.

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um

maintainer
#7 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Anton Ivanov

In first read - you do not want to do that.

buffer_op is slow (even with all the tweaks we have done to that). It is one of the reasons UML userspace is slower than it should be.

The primary reason for that is that it never copies more than a page at a time and pages in/out a page at a time.

Quoted 8 lines
Quoted 3 lines

We retain reasonable kernel speed because we bypass it for kernel - "if (uaccess_kernel())".

Unless I am missing something, this change will use the slow path currently used for userspace for the kernel.

uaccess_kernel() is not the fast path, it is the exception path when someone uses the uaccess helper on kernel pointer after doing a set_fs(KERNEL_DS). And this path is entirel gone once CONFIG_SET_FS is not set and replaced with calls to __get_kernel_nofault / __put_kernel_nofault.

OK. Cool.

Quoted 3 lines

In other words - very little generated code actually changes with this patch, except for the context switch path, which gets simplified.

I am going to test it and play with it later.

Thanks for the explanation.

Brgds,

Quoted 6 lines

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um

Signature · 9 lines
Anton R. Ivanov
Cambridgegreys Limited. Registered in England. Company Number 10273661
https://www.cambridgegreys.com/


_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
#8 lore raw

----- Ursprüngliche Mail -----

Quoted 4 lines
Quoted 2 lines

In other words - very little generated code actually changes with this patch, except for the context switch path, which gets simplified.

I am going to test it and play with it later.

Did it survive your testing?
The changes look sane to me.

Thanks,
//richard

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um

maintainer
#9 lore raw
Quoted 4 lines · Richard Weinberger

----- Ursprüngliche Mail -----

Quoted 3 lines
Quoted 2 lines

In other words - very little generated code actually changes with this patch, except for the context switch path, which gets simplified.

Quoted 8 lines
Quoted 1 line

I am going to test it and play with it later.

Did it survive your testing?
The changes look sane to me.

Thanks,
//richard

Does not crash, does not show any substantial differences in performance.

I am OK with it.

Signature · 8 lines
Anton R. Ivanov
Cambridgegreys Limited. Registered in England. Company Number 10273661
https://www.cambridgegreys.com/

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
#10 lore raw

----- Ursprüngliche Mail -----

Quoted 13 lines

Von: "anton ivanov" <[email protected]>
An: "richard" <[email protected]>
CC: "hch" <[email protected]>, "Johannes Berg" <[email protected]>, "linux-um" <[email protected]>
Gesendet: Donnerstag, 26. August 2021 23:12:32
Betreff: Re: [PATCH, RFC] um: remove set_fs

Quoted 6 lines · Richard Weinberger

----- Ursprüngliche Mail -----

Quoted 4 lines
Quoted 2 lines

In other words - very little generated code actually changes with this patch, except for the context switch path, which gets simplified.

I am going to test it and play with it later.

Quoted 8 lines
Quoted 6 lines

Did it survive your testing?
The changes look sane to me.

Thanks,
//richard

Does not crash, does not show any substantial differences in performance.

Sounds like a decent patch. :-)

Quoted 1 line

I am OK with it.

So, let's be keen and merge it.

Thanks,
//richard

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um

author
#11 lore raw
Quoted 5 lines · Richard Weinberger

Sounds like a decent patch. :-)

Quoted 1 line

I am OK with it.

So, let's be keen and merge it.

With all the merge conflicts including the one with the asm-generic tree that causes the TASK_SIZE_MAX redefinition it might be worth to drop it and merge it for the next merge window instead.

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um