[PATCH] uaccess: Add __copy_from_kernel_inatomic() primitive

Ingo Molnar
2015-04-10T09:22:01Z

* Ingo Molnar <mingo@kernel.org> wrote:

> Yeah, so what I missed here are those nops: placeholders for the
> STAC/CLAC instructions on x86... and this is what Linus mentioned
> about the clac() overhead.
>
> But this could be solved I think: by adding a
> copy_from_kernel_inatomic() primitive which simply leaves out the
> STAC/CLAC sequence: as these are always guaranteed to be kernel
> addresses, the SMAP fault should not be generated.

So the first step would be to introduce a generic
__copy_from_kernel_inatomic() primitive as attached below.

The next patch will implement efficient __copy_from_kernel_inatomic()
for x86.

Thanks,

Ingo

==================================>
>From 89b2ac882933947513c0aabd38e6b6c5a203c337 Mon Sep 17 00:00:00 2001
From: Ingo Molnar <mingo@kernel.org>
Date: Fri, 10 Apr 2015 11:19:23 +0200
Subject: [PATCH] uaccess: Add __copy_from_kernel_inatomic() primitive

Most architectures can just reuse __copy_from_user_inatomic() to
copy possibly-faulting data from known-valid kernel addresses.

Not-Yet-Signed-off-by: Ingo Molnar <mingo@kernel.org>
---
include/linux/uaccess.h | 12 ++++++++++++
kernel/locking/mutex.c | 3 ++-
2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index ecd3319dac33..885eea43b69f 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -107,4 +107,16 @@ extern long __probe_kernel_read(void *dst, const void *src, size_t size);
extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);

+/*
+ * Generic wrapper, most architectures can just use __copy_from_user_inatomic()
+ * to implement __copy_from_kernel_inatomic():
+ */
+#ifndef ARCH_HAS_COPY_FROM_KERNEL_INATOMIC
+static __must_check __always_inline int
+__copy_from_kernel_inatomic(void *dst, const void __user *src, unsigned size)
+{
+ return __copy_from_user_inatomic(dst, src, size);
+}
+#endif
+
#endif /* __LINUX_UACCESS_H__ */
diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index fcc7db45d62e..a4f74cda9fc4 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -26,6 +26,7 @@
#include <linux/interrupt.h>
#include <linux/debug_locks.h>
#include <linux/osq_lock.h>
+#include <linux/uaccess.h>

/*
* In the DEBUG case we are using the "NULL fastpath" for mutexes,
@@ -251,7 +252,7 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)
* NOTE2: We ignore failed copies, as the next iteration will clean
* up after us. This saves an extra branch in the common case.
*/
- ret = __copy_from_user_inatomic(&on_cpu, &owner->on_cpu, sizeof(on_cpu));
+ ret = __copy_from_kernel_inatomic(&on_cpu, &owner->on_cpu, sizeof(on_cpu));

if (!on_cpu || need_resched())
return false;