License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 15:07:57 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
* S390 version
|
2012-07-20 11:15:04 +02:00
|
|
|
* Copyright IBM Corp. 1999, 2000
|
2005-04-16 15:20:36 -07:00
|
|
|
* Author(s): Hartmut Penner (hp@de.ibm.com),
|
2022-05-29 21:36:16 +02:00
|
|
|
* Martin Schwidefsky (schwidefsky@de.ibm.com)
|
2005-04-16 15:20:36 -07:00
|
|
|
*
|
|
|
|
* Derived from "include/asm-i386/uaccess.h"
|
|
|
|
*/
|
|
|
|
#ifndef __S390_UACCESS_H
|
|
|
|
#define __S390_UACCESS_H
|
|
|
|
|
|
|
|
/*
|
|
|
|
* User space memory access functions
|
|
|
|
*/
|
2025-02-11 20:19:30 +01:00
|
|
|
#include <linux/pgtable.h>
|
2022-02-28 11:22:12 +01:00
|
|
|
#include <asm/asm-extable.h>
|
2017-02-17 08:13:28 +01:00
|
|
|
#include <asm/processor.h>
|
2016-12-26 00:48:37 -05:00
|
|
|
#include <asm/extable.h>
|
s390: remove all code using the access register mode
The vdso code for the getcpu() and the clock_gettime() call use the access
register mode to access the per-CPU vdso data page with the current code.
An alternative to the complicated AR mode is to use the secondary space
mode. This makes the vdso faster and quite a bit simpler. The downside is
that the uaccess code has to be changed quite a bit.
Which instructions are used depends on the machine and what kind of uaccess
operation is requested. The instruction dictates which ASCE value needs
to be loaded into %cr1 and %cr7.
The different cases:
* User copy with MVCOS for z10 and newer machines
The MVCOS instruction can copy between the primary space (aka user) and
the home space (aka kernel) directly. For set_fs(KERNEL_DS) the kernel
ASCE is loaded into %cr1. For set_fs(USER_DS) the user space is already
loaded in %cr1.
* User copy with MVCP/MVCS for older machines
To be able to execute the MVCP/MVCS instructions the kernel needs to
switch to primary mode. The control register %cr1 has to be set to the
kernel ASCE and %cr7 to either the kernel ASCE or the user ASCE dependent
on set_fs(KERNEL_DS) vs set_fs(USER_DS).
* Data access in the user address space for strnlen / futex
To use "normal" instruction with data from the user address space the
secondary space mode is used. The kernel needs to switch to primary mode,
%cr1 has to contain the kernel ASCE and %cr7 either the user ASCE or the
kernel ASCE, dependent on set_fs.
To load a new value into %cr1 or %cr7 is an expensive operation, the kernel
tries to be lazy about it. E.g. for multiple user copies in a row with
MVCP/MVCS the replacement of the vdso ASCE in %cr7 with the user ASCE is
done only once. On return to user space a CPU bit is checked that loads the
vdso ASCE again.
To enable and disable the data access via the secondary space two new
functions are added, enable_sacf_uaccess and disable_sacf_uaccess. The fact
that a context is in secondary space uaccess mode is stored in the
mm_segment_t value for the task. The code of an interrupt may use set_fs
as long as it returns to the previous state it got with get_fs with another
call to set_fs. The code in finish_arch_post_lock_switch simply has to do a
set_fs with the current mm_segment_t value for the task.
For CPUs with MVCOS:
CPU running in | %cr1 ASCE | %cr7 ASCE |
--------------------------------------|-----------|-----------|
user space | user | vdso |
kernel, USER_DS, normal-mode | user | vdso |
kernel, USER_DS, normal-mode, lazy | user | user |
kernel, USER_DS, sacf-mode | kernel | user |
kernel, KERNEL_DS, normal-mode | kernel | vdso |
kernel, KERNEL_DS, normal-mode, lazy | kernel | kernel |
kernel, KERNEL_DS, sacf-mode | kernel | kernel |
For CPUs without MVCOS:
CPU running in | %cr1 ASCE | %cr7 ASCE |
--------------------------------------|-----------|-----------|
user space | user | vdso |
kernel, USER_DS, normal-mode | user | vdso |
kernel, USER_DS, normal-mode lazy | kernel | user |
kernel, USER_DS, sacf-mode | kernel | user |
kernel, KERNEL_DS, normal-mode | kernel | vdso |
kernel, KERNEL_DS, normal-mode, lazy | kernel | kernel |
kernel, KERNEL_DS, sacf-mode | kernel | kernel |
The lines with "lazy" refer to the state after a copy via the secondary
space with a delayed reload of %cr1 and %cr7.
There are three hardware address spaces that can cause a DAT exception,
primary, secondary and home space. The exception can be related to
four different fault types: user space fault, vdso fault, kernel fault,
and the gmap faults.
Dependent on the set_fs state and normal vs. sacf mode there are a number
of fault combinations:
1) user address space fault via the primary ASCE
2) gmap address space fault via the primary ASCE
3) kernel address space fault via the primary ASCE for machines with
MVCOS and set_fs(KERNEL_DS)
4) vdso address space faults via the secondary ASCE with an invalid
address while running in secondary space in problem state
5) user address space fault via the secondary ASCE for user-copy
based on the secondary space mode, e.g. futex_ops or strnlen_user
6) kernel address space fault via the secondary ASCE for user-copy
with secondary space mode with set_fs(KERNEL_DS)
7) kernel address space fault via the primary ASCE for user-copy
with secondary space mode with set_fs(USER_DS) on machines without
MVCOS.
8) kernel address space fault via the home space ASCE
Replace user_space_fault() with a new function get_fault_type() that
can distinguish all four different fault types.
With these changes the futex atomic ops from the kernel and the
strnlen_user will get a little bit slower, as well as the old style
uaccess with MVCP/MVCS. All user accesses based on MVCOS will be as
fast as before. On the positive side, the user space vdso code is a
lot faster and Linux ceases to use the complicated AR mode.
Reviewed-by: Heiko Carstens <heiko.carstens@de.ibm.com>
Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
2017-08-22 12:08:22 +02:00
|
|
|
#include <asm/facility.h>
|
uaccess: generalize access_ok()
There are many different ways that access_ok() is defined across
architectures, but in the end, they all just compare against the
user_addr_max() value or they accept anything.
Provide one definition that works for most architectures, checking
against TASK_SIZE_MAX for user processes or skipping the check inside
of uaccess_kernel() sections.
For architectures without CONFIG_SET_FS(), this should be the fastest
check, as it comes down to a single comparison of a pointer against a
compile-time constant, while the architecture specific versions tend to
do something more complex for historic reasons or get something wrong.
Type checking for __user annotations is handled inconsistently across
architectures, but this is easily simplified as well by using an inline
function that takes a 'const void __user *' argument. A handful of
callers need an extra __user annotation for this.
Some architectures had trick to use 33-bit or 65-bit arithmetic on the
addresses to calculate the overflow, however this simpler version uses
fewer registers, which means it can produce better object code in the
end despite needing a second (statically predicted) branch.
Reviewed-by: Christoph Hellwig <hch@lst.de>
Acked-by: Mark Rutland <mark.rutland@arm.com> [arm64, asm-generic]
Acked-by: Geert Uytterhoeven <geert@linux-m68k.org>
Acked-by: Stafford Horne <shorne@gmail.com>
Acked-by: Dinh Nguyen <dinguyen@kernel.org>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
2022-02-15 17:55:04 +01:00
|
|
|
#include <asm-generic/access_ok.h>
|
2025-04-09 15:01:50 +02:00
|
|
|
#include <asm/asce.h>
|
2024-06-21 13:35:19 +02:00
|
|
|
#include <linux/instrumented.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2020-11-21 11:14:56 +01:00
|
|
|
void debug_user_asce(int exit);
|
2020-11-16 08:06:41 +01:00
|
|
|
|
2025-02-11 20:19:26 +01:00
|
|
|
#ifdef CONFIG_KMSAN
|
|
|
|
#define uaccess_kmsan_or_inline noinline __maybe_unused __no_sanitize_memory
|
|
|
|
#else
|
|
|
|
#define uaccess_kmsan_or_inline __always_inline
|
|
|
|
#endif
|
|
|
|
|
2025-02-11 20:19:28 +01:00
|
|
|
#define INLINE_COPY_FROM_USER
|
|
|
|
#define INLINE_COPY_TO_USER
|
|
|
|
|
2025-02-11 20:19:26 +01:00
|
|
|
static uaccess_kmsan_or_inline __must_check unsigned long
|
2025-02-11 20:19:27 +01:00
|
|
|
raw_copy_from_user(void *to, const void __user *from, unsigned long size)
|
2025-01-16 16:24:31 +01:00
|
|
|
{
|
2025-02-11 20:19:26 +01:00
|
|
|
unsigned long osize;
|
|
|
|
int cc;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
osize = size;
|
|
|
|
asm_inline volatile(
|
2025-02-11 20:19:27 +01:00
|
|
|
" lhi %%r0,%[spec]\n"
|
2025-02-11 20:19:26 +01:00
|
|
|
"0: mvcos %[to],%[from],%[size]\n"
|
|
|
|
"1: nopr %%r7\n"
|
|
|
|
CC_IPM(cc)
|
|
|
|
EX_TABLE_UA_MVCOS_FROM(0b, 0b)
|
|
|
|
EX_TABLE_UA_MVCOS_FROM(1b, 0b)
|
|
|
|
: CC_OUT(cc, cc), [size] "+d" (size), [to] "=Q" (*(char *)to)
|
2025-02-11 20:19:27 +01:00
|
|
|
: [spec] "I" (0x81), [from] "Q" (*(const char __user *)from)
|
2025-02-11 20:19:26 +01:00
|
|
|
: CC_CLOBBER_LIST("memory", "0"));
|
2025-02-11 20:19:29 +01:00
|
|
|
if (__builtin_constant_p(osize) && osize <= 4096)
|
|
|
|
return osize - size;
|
2025-02-11 20:19:26 +01:00
|
|
|
if (likely(CC_TRANSFORM(cc) == 0))
|
|
|
|
return osize - size;
|
|
|
|
size -= 4096;
|
|
|
|
to += 4096;
|
|
|
|
from += 4096;
|
|
|
|
}
|
2025-01-16 16:24:31 +01:00
|
|
|
}
|
|
|
|
|
2025-02-11 20:19:26 +01:00
|
|
|
static uaccess_kmsan_or_inline __must_check unsigned long
|
2025-02-11 20:19:27 +01:00
|
|
|
raw_copy_to_user(void __user *to, const void *from, unsigned long size)
|
2025-01-16 16:24:31 +01:00
|
|
|
{
|
2025-02-11 20:19:26 +01:00
|
|
|
unsigned long osize;
|
|
|
|
int cc;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
osize = size;
|
|
|
|
asm_inline volatile(
|
2025-02-11 20:19:27 +01:00
|
|
|
" llilh %%r0,%[spec]\n"
|
2025-02-11 20:19:26 +01:00
|
|
|
"0: mvcos %[to],%[from],%[size]\n"
|
|
|
|
"1: nopr %%r7\n"
|
|
|
|
CC_IPM(cc)
|
|
|
|
EX_TABLE_UA_MVCOS_TO(0b, 0b)
|
|
|
|
EX_TABLE_UA_MVCOS_TO(1b, 0b)
|
|
|
|
: CC_OUT(cc, cc), [size] "+d" (size), [to] "=Q" (*(char __user *)to)
|
2025-02-11 20:19:27 +01:00
|
|
|
: [spec] "I" (0x81), [from] "Q" (*(const char *)from)
|
2025-02-11 20:19:26 +01:00
|
|
|
: CC_CLOBBER_LIST("memory", "0"));
|
2025-02-11 20:19:29 +01:00
|
|
|
if (__builtin_constant_p(osize) && osize <= 4096)
|
|
|
|
return osize - size;
|
2025-02-11 20:19:26 +01:00
|
|
|
if (likely(CC_TRANSFORM(cc) == 0))
|
|
|
|
return osize - size;
|
|
|
|
size -= 4096;
|
|
|
|
to += 4096;
|
|
|
|
from += 4096;
|
|
|
|
}
|
2025-01-16 16:24:31 +01:00
|
|
|
}
|
|
|
|
|
2022-02-11 19:22:06 +01:00
|
|
|
unsigned long __must_check
|
|
|
|
_copy_from_user_key(void *to, const void __user *from, unsigned long n, unsigned long key);
|
|
|
|
|
|
|
|
static __always_inline unsigned long __must_check
|
|
|
|
copy_from_user_key(void *to, const void __user *from, unsigned long n, unsigned long key)
|
|
|
|
{
|
2022-06-06 18:42:59 -04:00
|
|
|
if (check_copy_size(to, n, false))
|
2022-02-11 19:22:06 +01:00
|
|
|
n = _copy_from_user_key(to, from, n, key);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long __must_check
|
|
|
|
_copy_to_user_key(void __user *to, const void *from, unsigned long n, unsigned long key);
|
|
|
|
|
|
|
|
static __always_inline unsigned long __must_check
|
|
|
|
copy_to_user_key(void __user *to, const void *from, unsigned long n, unsigned long key)
|
|
|
|
{
|
2022-06-06 18:42:59 -04:00
|
|
|
if (check_copy_size(from, n, true))
|
2022-02-11 19:22:06 +01:00
|
|
|
n = _copy_to_user_key(to, from, n, key);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2022-05-29 21:27:48 +02:00
|
|
|
int __noreturn __put_user_bad(void);
|
|
|
|
|
2025-01-13 13:37:06 +01:00
|
|
|
#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
|
|
|
|
|
|
|
|
#define DEFINE_PUT_USER_NOINSTR(type) \
|
2025-01-21 15:55:02 +01:00
|
|
|
static uaccess_kmsan_or_inline int \
|
2025-01-13 13:37:06 +01:00
|
|
|
__put_user_##type##_noinstr(unsigned type __user *to, \
|
|
|
|
unsigned type *from, \
|
|
|
|
unsigned long size) \
|
|
|
|
{ \
|
|
|
|
asm goto( \
|
|
|
|
" llilh %%r0,%[spec]\n" \
|
|
|
|
"0: mvcos %[to],%[from],%[size]\n" \
|
|
|
|
"1: nopr %%r7\n" \
|
|
|
|
EX_TABLE(0b, %l[Efault]) \
|
|
|
|
EX_TABLE(1b, %l[Efault]) \
|
|
|
|
: [to] "+Q" (*to) \
|
|
|
|
: [size] "d" (size), [from] "Q" (*from), \
|
|
|
|
[spec] "I" (0x81) \
|
|
|
|
: "cc", "0" \
|
|
|
|
: Efault \
|
|
|
|
); \
|
|
|
|
return 0; \
|
|
|
|
Efault: \
|
|
|
|
return -EFAULT; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
|
|
|
|
|
|
|
|
#define DEFINE_PUT_USER_NOINSTR(type) \
|
2025-01-21 15:55:02 +01:00
|
|
|
static uaccess_kmsan_or_inline int \
|
s390/uaccess: add KMSAN support to put_user() and get_user()
put_user() uses inline assembly with precise constraints, so Clang is in
principle capable of instrumenting it automatically. Unfortunately, one
of the constraints contains a dereferenced user pointer, and Clang does
not currently distinguish user and kernel pointers. Therefore KMSAN
attempts to access shadow for user pointers, which is not a right thing to
do.
An obvious fix to add __no_sanitize_memory to __put_user_fn() does not
work, since it's __always_inline. And __always_inline cannot be removed
due to the __put_user_bad() trick.
A different obvious fix of using the "a" instead of the "+Q" constraint
degrades the code quality, which is very important here, since it's a hot
path.
Instead, repurpose the __put_user_asm() macro to define
__put_user_{char,short,int,long}_noinstr() functions and mark them with
__no_sanitize_memory. For the non-KMSAN builds make them __always_inline
in order to keep the generated code quality. Also define
__put_user_{char,short,int,long}() functions, which call the
aforementioned ones and which *are* instrumented, because they call KMSAN
hooks, which may be implemented as macros.
The same applies to get_user() as well.
Link: https://lkml.kernel.org/r/20240621113706.315500-35-iii@linux.ibm.com
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Acked-by: Heiko Carstens <hca@linux.ibm.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: <kasan-dev@googlegroups.com>
Cc: Marco Elver <elver@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2024-06-21 13:35:18 +02:00
|
|
|
__put_user_##type##_noinstr(unsigned type __user *to, \
|
|
|
|
unsigned type *from, \
|
|
|
|
unsigned long size) \
|
|
|
|
{ \
|
|
|
|
int rc; \
|
2022-01-11 11:00:03 +01:00
|
|
|
\
|
2025-03-17 16:22:35 +01:00
|
|
|
asm_inline volatile( \
|
2025-01-13 13:37:05 +01:00
|
|
|
" llilh %%r0,%[spec]\n" \
|
2025-01-13 13:37:03 +01:00
|
|
|
"0: mvcos %[to],%[from],%[size]\n" \
|
|
|
|
"1: lhi %[rc],0\n" \
|
2022-01-11 11:00:03 +01:00
|
|
|
"2:\n" \
|
2025-01-13 13:36:58 +01:00
|
|
|
EX_TABLE_UA_FAULT(0b, 2b, %[rc]) \
|
|
|
|
EX_TABLE_UA_FAULT(1b, 2b, %[rc]) \
|
2025-01-13 13:37:03 +01:00
|
|
|
: [rc] "=d" (rc), [to] "+Q" (*to) \
|
|
|
|
: [size] "d" (size), [from] "Q" (*from), \
|
2025-01-13 13:37:05 +01:00
|
|
|
[spec] "I" (0x81) \
|
2022-01-11 11:00:03 +01:00
|
|
|
: "cc", "0"); \
|
s390/uaccess: add KMSAN support to put_user() and get_user()
put_user() uses inline assembly with precise constraints, so Clang is in
principle capable of instrumenting it automatically. Unfortunately, one
of the constraints contains a dereferenced user pointer, and Clang does
not currently distinguish user and kernel pointers. Therefore KMSAN
attempts to access shadow for user pointers, which is not a right thing to
do.
An obvious fix to add __no_sanitize_memory to __put_user_fn() does not
work, since it's __always_inline. And __always_inline cannot be removed
due to the __put_user_bad() trick.
A different obvious fix of using the "a" instead of the "+Q" constraint
degrades the code quality, which is very important here, since it's a hot
path.
Instead, repurpose the __put_user_asm() macro to define
__put_user_{char,short,int,long}_noinstr() functions and mark them with
__no_sanitize_memory. For the non-KMSAN builds make them __always_inline
in order to keep the generated code quality. Also define
__put_user_{char,short,int,long}() functions, which call the
aforementioned ones and which *are* instrumented, because they call KMSAN
hooks, which may be implemented as macros.
The same applies to get_user() as well.
Link: https://lkml.kernel.org/r/20240621113706.315500-35-iii@linux.ibm.com
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Acked-by: Heiko Carstens <hca@linux.ibm.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: <kasan-dev@googlegroups.com>
Cc: Marco Elver <elver@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2024-06-21 13:35:18 +02:00
|
|
|
return rc; \
|
2025-01-13 13:37:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
|
|
|
|
|
|
|
|
DEFINE_PUT_USER_NOINSTR(char);
|
|
|
|
DEFINE_PUT_USER_NOINSTR(short);
|
|
|
|
DEFINE_PUT_USER_NOINSTR(int);
|
|
|
|
DEFINE_PUT_USER_NOINSTR(long);
|
|
|
|
|
|
|
|
#define DEFINE_PUT_USER(type) \
|
s390/uaccess: add KMSAN support to put_user() and get_user()
put_user() uses inline assembly with precise constraints, so Clang is in
principle capable of instrumenting it automatically. Unfortunately, one
of the constraints contains a dereferenced user pointer, and Clang does
not currently distinguish user and kernel pointers. Therefore KMSAN
attempts to access shadow for user pointers, which is not a right thing to
do.
An obvious fix to add __no_sanitize_memory to __put_user_fn() does not
work, since it's __always_inline. And __always_inline cannot be removed
due to the __put_user_bad() trick.
A different obvious fix of using the "a" instead of the "+Q" constraint
degrades the code quality, which is very important here, since it's a hot
path.
Instead, repurpose the __put_user_asm() macro to define
__put_user_{char,short,int,long}_noinstr() functions and mark them with
__no_sanitize_memory. For the non-KMSAN builds make them __always_inline
in order to keep the generated code quality. Also define
__put_user_{char,short,int,long}() functions, which call the
aforementioned ones and which *are* instrumented, because they call KMSAN
hooks, which may be implemented as macros.
The same applies to get_user() as well.
Link: https://lkml.kernel.org/r/20240621113706.315500-35-iii@linux.ibm.com
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Acked-by: Heiko Carstens <hca@linux.ibm.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: <kasan-dev@googlegroups.com>
Cc: Marco Elver <elver@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2024-06-21 13:35:18 +02:00
|
|
|
static __always_inline int \
|
|
|
|
__put_user_##type(unsigned type __user *to, unsigned type *from, \
|
|
|
|
unsigned long size) \
|
|
|
|
{ \
|
|
|
|
int rc; \
|
|
|
|
\
|
|
|
|
rc = __put_user_##type##_noinstr(to, from, size); \
|
|
|
|
instrument_put_user(*from, to, size); \
|
|
|
|
return rc; \
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_PUT_USER(char);
|
|
|
|
DEFINE_PUT_USER(short);
|
|
|
|
DEFINE_PUT_USER(int);
|
|
|
|
DEFINE_PUT_USER(long);
|
2014-04-17 14:16:03 +02:00
|
|
|
|
2025-01-13 13:37:01 +01:00
|
|
|
#define __put_user(x, ptr) \
|
|
|
|
({ \
|
|
|
|
__typeof__(*(ptr)) __x = (x); \
|
2025-01-13 13:37:02 +01:00
|
|
|
int __prc; \
|
2025-01-13 13:37:01 +01:00
|
|
|
\
|
|
|
|
__chk_user_ptr(ptr); \
|
|
|
|
switch (sizeof(*(ptr))) { \
|
|
|
|
case 1: \
|
2025-01-13 13:37:02 +01:00
|
|
|
__prc = __put_user_char((unsigned char __user *)(ptr), \
|
|
|
|
(unsigned char *)&__x, \
|
|
|
|
sizeof(*(ptr))); \
|
|
|
|
break; \
|
2025-01-13 13:37:01 +01:00
|
|
|
case 2: \
|
2025-01-13 13:37:02 +01:00
|
|
|
__prc = __put_user_short((unsigned short __user *)(ptr),\
|
|
|
|
(unsigned short *)&__x, \
|
|
|
|
sizeof(*(ptr))); \
|
|
|
|
break; \
|
2025-01-13 13:37:01 +01:00
|
|
|
case 4: \
|
2025-01-13 13:37:02 +01:00
|
|
|
__prc = __put_user_int((unsigned int __user *)(ptr), \
|
|
|
|
(unsigned int *)&__x, \
|
|
|
|
sizeof(*(ptr))); \
|
|
|
|
break; \
|
2025-01-13 13:37:01 +01:00
|
|
|
case 8: \
|
2025-01-13 13:37:02 +01:00
|
|
|
__prc = __put_user_long((unsigned long __user *)(ptr), \
|
|
|
|
(unsigned long *)&__x, \
|
|
|
|
sizeof(*(ptr))); \
|
2025-01-13 13:37:01 +01:00
|
|
|
break; \
|
|
|
|
default: \
|
2025-01-13 13:37:02 +01:00
|
|
|
__prc = __put_user_bad(); \
|
2025-01-13 13:37:01 +01:00
|
|
|
break; \
|
|
|
|
} \
|
2025-01-13 13:37:02 +01:00
|
|
|
__builtin_expect(__prc, 0); \
|
2025-01-13 13:37:01 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
#define put_user(x, ptr) \
|
|
|
|
({ \
|
|
|
|
might_fault(); \
|
|
|
|
__put_user(x, ptr); \
|
|
|
|
})
|
|
|
|
|
2022-05-29 21:27:48 +02:00
|
|
|
int __noreturn __get_user_bad(void);
|
|
|
|
|
2025-01-13 13:37:06 +01:00
|
|
|
#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
|
|
|
|
|
|
|
|
#define DEFINE_GET_USER_NOINSTR(type) \
|
2025-01-21 15:55:02 +01:00
|
|
|
static uaccess_kmsan_or_inline int \
|
2025-01-13 13:37:06 +01:00
|
|
|
__get_user_##type##_noinstr(unsigned type *to, \
|
|
|
|
const unsigned type __user *from, \
|
|
|
|
unsigned long size) \
|
|
|
|
{ \
|
|
|
|
asm goto( \
|
|
|
|
" lhi %%r0,%[spec]\n" \
|
|
|
|
"0: mvcos %[to],%[from],%[size]\n" \
|
|
|
|
"1: nopr %%r7\n" \
|
|
|
|
EX_TABLE(0b, %l[Efault]) \
|
|
|
|
EX_TABLE(1b, %l[Efault]) \
|
|
|
|
: [to] "=Q" (*to) \
|
|
|
|
: [size] "d" (size), [from] "Q" (*from), \
|
|
|
|
[spec] "I" (0x81) \
|
|
|
|
: "cc", "0" \
|
|
|
|
: Efault \
|
|
|
|
); \
|
|
|
|
return 0; \
|
|
|
|
Efault: \
|
|
|
|
*to = 0; \
|
|
|
|
return -EFAULT; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
|
|
|
|
|
|
|
|
#define DEFINE_GET_USER_NOINSTR(type) \
|
2025-01-21 15:55:02 +01:00
|
|
|
static uaccess_kmsan_or_inline int \
|
s390/uaccess: add KMSAN support to put_user() and get_user()
put_user() uses inline assembly with precise constraints, so Clang is in
principle capable of instrumenting it automatically. Unfortunately, one
of the constraints contains a dereferenced user pointer, and Clang does
not currently distinguish user and kernel pointers. Therefore KMSAN
attempts to access shadow for user pointers, which is not a right thing to
do.
An obvious fix to add __no_sanitize_memory to __put_user_fn() does not
work, since it's __always_inline. And __always_inline cannot be removed
due to the __put_user_bad() trick.
A different obvious fix of using the "a" instead of the "+Q" constraint
degrades the code quality, which is very important here, since it's a hot
path.
Instead, repurpose the __put_user_asm() macro to define
__put_user_{char,short,int,long}_noinstr() functions and mark them with
__no_sanitize_memory. For the non-KMSAN builds make them __always_inline
in order to keep the generated code quality. Also define
__put_user_{char,short,int,long}() functions, which call the
aforementioned ones and which *are* instrumented, because they call KMSAN
hooks, which may be implemented as macros.
The same applies to get_user() as well.
Link: https://lkml.kernel.org/r/20240621113706.315500-35-iii@linux.ibm.com
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Acked-by: Heiko Carstens <hca@linux.ibm.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: <kasan-dev@googlegroups.com>
Cc: Marco Elver <elver@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2024-06-21 13:35:18 +02:00
|
|
|
__get_user_##type##_noinstr(unsigned type *to, \
|
2025-01-13 13:37:02 +01:00
|
|
|
const unsigned type __user *from, \
|
s390/uaccess: add KMSAN support to put_user() and get_user()
put_user() uses inline assembly with precise constraints, so Clang is in
principle capable of instrumenting it automatically. Unfortunately, one
of the constraints contains a dereferenced user pointer, and Clang does
not currently distinguish user and kernel pointers. Therefore KMSAN
attempts to access shadow for user pointers, which is not a right thing to
do.
An obvious fix to add __no_sanitize_memory to __put_user_fn() does not
work, since it's __always_inline. And __always_inline cannot be removed
due to the __put_user_bad() trick.
A different obvious fix of using the "a" instead of the "+Q" constraint
degrades the code quality, which is very important here, since it's a hot
path.
Instead, repurpose the __put_user_asm() macro to define
__put_user_{char,short,int,long}_noinstr() functions and mark them with
__no_sanitize_memory. For the non-KMSAN builds make them __always_inline
in order to keep the generated code quality. Also define
__put_user_{char,short,int,long}() functions, which call the
aforementioned ones and which *are* instrumented, because they call KMSAN
hooks, which may be implemented as macros.
The same applies to get_user() as well.
Link: https://lkml.kernel.org/r/20240621113706.315500-35-iii@linux.ibm.com
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Acked-by: Heiko Carstens <hca@linux.ibm.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: <kasan-dev@googlegroups.com>
Cc: Marco Elver <elver@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2024-06-21 13:35:18 +02:00
|
|
|
unsigned long size) \
|
|
|
|
{ \
|
|
|
|
int rc; \
|
2022-05-01 21:26:06 +02:00
|
|
|
\
|
2025-03-17 16:22:35 +01:00
|
|
|
asm_inline volatile( \
|
2025-01-13 13:37:05 +01:00
|
|
|
" lhi %%r0,%[spec]\n" \
|
2025-01-13 13:37:04 +01:00
|
|
|
"0: mvcos %[to],%[from],%[size]\n" \
|
2025-01-13 13:37:03 +01:00
|
|
|
"1: lhi %[rc],0\n" \
|
2022-05-01 21:26:06 +02:00
|
|
|
"2:\n" \
|
2025-01-13 13:37:04 +01:00
|
|
|
EX_TABLE_UA_FAULT(0b, 2b, %[rc]) \
|
|
|
|
EX_TABLE_UA_FAULT(1b, 2b, %[rc]) \
|
|
|
|
: [rc] "=d" (rc), [to] "=Q" (*to) \
|
2025-01-13 13:37:03 +01:00
|
|
|
: [size] "d" (size), [from] "Q" (*from), \
|
2025-01-13 13:37:05 +01:00
|
|
|
[spec] "I" (0x81) \
|
2022-05-01 21:26:06 +02:00
|
|
|
: "cc", "0"); \
|
2025-01-13 13:37:04 +01:00
|
|
|
if (likely(!rc)) \
|
|
|
|
return 0; \
|
|
|
|
*to = 0; \
|
s390/uaccess: add KMSAN support to put_user() and get_user()
put_user() uses inline assembly with precise constraints, so Clang is in
principle capable of instrumenting it automatically. Unfortunately, one
of the constraints contains a dereferenced user pointer, and Clang does
not currently distinguish user and kernel pointers. Therefore KMSAN
attempts to access shadow for user pointers, which is not a right thing to
do.
An obvious fix to add __no_sanitize_memory to __put_user_fn() does not
work, since it's __always_inline. And __always_inline cannot be removed
due to the __put_user_bad() trick.
A different obvious fix of using the "a" instead of the "+Q" constraint
degrades the code quality, which is very important here, since it's a hot
path.
Instead, repurpose the __put_user_asm() macro to define
__put_user_{char,short,int,long}_noinstr() functions and mark them with
__no_sanitize_memory. For the non-KMSAN builds make them __always_inline
in order to keep the generated code quality. Also define
__put_user_{char,short,int,long}() functions, which call the
aforementioned ones and which *are* instrumented, because they call KMSAN
hooks, which may be implemented as macros.
The same applies to get_user() as well.
Link: https://lkml.kernel.org/r/20240621113706.315500-35-iii@linux.ibm.com
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Acked-by: Heiko Carstens <hca@linux.ibm.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: <kasan-dev@googlegroups.com>
Cc: Marco Elver <elver@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2024-06-21 13:35:18 +02:00
|
|
|
return rc; \
|
2025-01-13 13:37:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
|
|
|
|
|
|
|
|
DEFINE_GET_USER_NOINSTR(char);
|
|
|
|
DEFINE_GET_USER_NOINSTR(short);
|
|
|
|
DEFINE_GET_USER_NOINSTR(int);
|
|
|
|
DEFINE_GET_USER_NOINSTR(long);
|
|
|
|
|
|
|
|
#define DEFINE_GET_USER(type) \
|
s390/uaccess: add KMSAN support to put_user() and get_user()
put_user() uses inline assembly with precise constraints, so Clang is in
principle capable of instrumenting it automatically. Unfortunately, one
of the constraints contains a dereferenced user pointer, and Clang does
not currently distinguish user and kernel pointers. Therefore KMSAN
attempts to access shadow for user pointers, which is not a right thing to
do.
An obvious fix to add __no_sanitize_memory to __put_user_fn() does not
work, since it's __always_inline. And __always_inline cannot be removed
due to the __put_user_bad() trick.
A different obvious fix of using the "a" instead of the "+Q" constraint
degrades the code quality, which is very important here, since it's a hot
path.
Instead, repurpose the __put_user_asm() macro to define
__put_user_{char,short,int,long}_noinstr() functions and mark them with
__no_sanitize_memory. For the non-KMSAN builds make them __always_inline
in order to keep the generated code quality. Also define
__put_user_{char,short,int,long}() functions, which call the
aforementioned ones and which *are* instrumented, because they call KMSAN
hooks, which may be implemented as macros.
The same applies to get_user() as well.
Link: https://lkml.kernel.org/r/20240621113706.315500-35-iii@linux.ibm.com
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Acked-by: Heiko Carstens <hca@linux.ibm.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: <kasan-dev@googlegroups.com>
Cc: Marco Elver <elver@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2024-06-21 13:35:18 +02:00
|
|
|
static __always_inline int \
|
2025-01-13 13:37:02 +01:00
|
|
|
__get_user_##type(unsigned type *to, const unsigned type __user *from, \
|
s390/uaccess: add KMSAN support to put_user() and get_user()
put_user() uses inline assembly with precise constraints, so Clang is in
principle capable of instrumenting it automatically. Unfortunately, one
of the constraints contains a dereferenced user pointer, and Clang does
not currently distinguish user and kernel pointers. Therefore KMSAN
attempts to access shadow for user pointers, which is not a right thing to
do.
An obvious fix to add __no_sanitize_memory to __put_user_fn() does not
work, since it's __always_inline. And __always_inline cannot be removed
due to the __put_user_bad() trick.
A different obvious fix of using the "a" instead of the "+Q" constraint
degrades the code quality, which is very important here, since it's a hot
path.
Instead, repurpose the __put_user_asm() macro to define
__put_user_{char,short,int,long}_noinstr() functions and mark them with
__no_sanitize_memory. For the non-KMSAN builds make them __always_inline
in order to keep the generated code quality. Also define
__put_user_{char,short,int,long}() functions, which call the
aforementioned ones and which *are* instrumented, because they call KMSAN
hooks, which may be implemented as macros.
The same applies to get_user() as well.
Link: https://lkml.kernel.org/r/20240621113706.315500-35-iii@linux.ibm.com
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Acked-by: Heiko Carstens <hca@linux.ibm.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: <kasan-dev@googlegroups.com>
Cc: Marco Elver <elver@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2024-06-21 13:35:18 +02:00
|
|
|
unsigned long size) \
|
|
|
|
{ \
|
|
|
|
int rc; \
|
|
|
|
\
|
|
|
|
rc = __get_user_##type##_noinstr(to, from, size); \
|
|
|
|
instrument_get_user(*to); \
|
|
|
|
return rc; \
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_GET_USER(char);
|
|
|
|
DEFINE_GET_USER(short);
|
|
|
|
DEFINE_GET_USER(int);
|
|
|
|
DEFINE_GET_USER(long);
|
2022-05-01 21:26:06 +02:00
|
|
|
|
2022-05-29 21:36:16 +02:00
|
|
|
#define __get_user(x, ptr) \
|
|
|
|
({ \
|
2025-01-13 13:37:02 +01:00
|
|
|
const __user void *____guptr = (ptr); \
|
|
|
|
int __grc; \
|
2022-05-29 21:36:16 +02:00
|
|
|
\
|
|
|
|
__chk_user_ptr(ptr); \
|
|
|
|
switch (sizeof(*(ptr))) { \
|
|
|
|
case 1: { \
|
2025-01-13 13:37:02 +01:00
|
|
|
const unsigned char __user *__guptr = ____guptr; \
|
2022-05-29 21:36:16 +02:00
|
|
|
unsigned char __x; \
|
|
|
|
\
|
2025-01-13 13:37:02 +01:00
|
|
|
__grc = __get_user_char(&__x, __guptr, sizeof(*(ptr))); \
|
2022-05-29 21:36:16 +02:00
|
|
|
(x) = *(__force __typeof__(*(ptr)) *)&__x; \
|
|
|
|
break; \
|
|
|
|
}; \
|
|
|
|
case 2: { \
|
2025-01-13 13:37:02 +01:00
|
|
|
const unsigned short __user *__guptr = ____guptr; \
|
2022-05-29 21:36:16 +02:00
|
|
|
unsigned short __x; \
|
|
|
|
\
|
2025-01-13 13:37:02 +01:00
|
|
|
__grc = __get_user_short(&__x, __guptr, sizeof(*(ptr)));\
|
2022-05-29 21:36:16 +02:00
|
|
|
(x) = *(__force __typeof__(*(ptr)) *)&__x; \
|
|
|
|
break; \
|
|
|
|
}; \
|
|
|
|
case 4: { \
|
2025-01-13 13:37:02 +01:00
|
|
|
const unsigned int __user *__guptr = ____guptr; \
|
2022-05-29 21:36:16 +02:00
|
|
|
unsigned int __x; \
|
|
|
|
\
|
2025-01-13 13:37:02 +01:00
|
|
|
__grc = __get_user_int(&__x, __guptr, sizeof(*(ptr))); \
|
2022-05-29 21:36:16 +02:00
|
|
|
(x) = *(__force __typeof__(*(ptr)) *)&__x; \
|
|
|
|
break; \
|
|
|
|
}; \
|
|
|
|
case 8: { \
|
2025-01-13 13:37:02 +01:00
|
|
|
const unsigned long __user *__guptr = ____guptr; \
|
2022-05-29 21:36:16 +02:00
|
|
|
unsigned long __x; \
|
|
|
|
\
|
2025-01-13 13:37:02 +01:00
|
|
|
__grc = __get_user_long(&__x, __guptr, sizeof(*(ptr))); \
|
2022-05-29 21:36:16 +02:00
|
|
|
(x) = *(__force __typeof__(*(ptr)) *)&__x; \
|
|
|
|
break; \
|
|
|
|
}; \
|
|
|
|
default: \
|
2025-01-13 13:37:02 +01:00
|
|
|
__grc = __get_user_bad(); \
|
2022-05-29 21:36:16 +02:00
|
|
|
break; \
|
|
|
|
} \
|
2025-01-13 13:37:02 +01:00
|
|
|
__builtin_expect(__grc, 0); \
|
2005-04-16 15:20:36 -07:00
|
|
|
})
|
|
|
|
|
2022-05-29 21:36:16 +02:00
|
|
|
#define get_user(x, ptr) \
|
|
|
|
({ \
|
|
|
|
might_fault(); \
|
|
|
|
__get_user(x, ptr); \
|
2005-04-16 15:20:36 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy a null terminated string from userspace.
|
|
|
|
*/
|
2021-07-22 22:07:30 +02:00
|
|
|
long __must_check strncpy_from_user(char *dst, const char __user *src, long count);
|
2014-01-23 11:18:36 +01:00
|
|
|
|
2021-07-22 22:07:30 +02:00
|
|
|
long __must_check strnlen_user(const char __user *src, long count);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2025-02-11 20:19:30 +01:00
|
|
|
static uaccess_kmsan_or_inline __must_check unsigned long
|
|
|
|
__clear_user(void __user *to, unsigned long size)
|
|
|
|
{
|
|
|
|
unsigned long osize;
|
|
|
|
int cc;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
osize = size;
|
|
|
|
asm_inline volatile(
|
|
|
|
" llilh %%r0,%[spec]\n"
|
|
|
|
"0: mvcos %[to],%[from],%[size]\n"
|
|
|
|
"1: nopr %%r7\n"
|
|
|
|
CC_IPM(cc)
|
|
|
|
EX_TABLE_UA_MVCOS_TO(0b, 0b)
|
|
|
|
EX_TABLE_UA_MVCOS_TO(1b, 0b)
|
|
|
|
: CC_OUT(cc, cc), [size] "+d" (size), [to] "=Q" (*(char __user *)to)
|
|
|
|
: [spec] "I" (0x81), [from] "Q" (*(const char *)empty_zero_page)
|
|
|
|
: CC_CLOBBER_LIST("memory", "0"));
|
|
|
|
if (__builtin_constant_p(osize) && osize <= 4096)
|
|
|
|
return osize - size;
|
|
|
|
if (CC_TRANSFORM(cc) == 0)
|
|
|
|
return osize - size;
|
|
|
|
size -= 4096;
|
|
|
|
to += 4096;
|
|
|
|
}
|
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2025-02-11 20:19:30 +01:00
|
|
|
static __always_inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2009-06-12 10:26:32 +02:00
|
|
|
might_fault();
|
2014-01-23 11:18:36 +01:00
|
|
|
return __clear_user(to, n);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
2024-07-16 13:50:52 +02:00
|
|
|
void *__s390_kernel_write(void *dst, const void *src, size_t size);
|
|
|
|
|
|
|
|
static inline void *s390_kernel_write(void *dst, const void *src, size_t size)
|
|
|
|
{
|
|
|
|
if (__is_defined(__DECOMPRESSOR))
|
|
|
|
return memcpy(dst, src, size);
|
|
|
|
return __s390_kernel_write(dst, src, size);
|
|
|
|
}
|
2012-03-28 18:30:02 +01:00
|
|
|
|
2025-01-13 13:36:59 +01:00
|
|
|
void __noreturn __mvc_kernel_nofault_bad(void);
|
2020-09-14 13:42:25 +02:00
|
|
|
|
2025-01-13 13:37:00 +01:00
|
|
|
#if defined(CONFIG_CC_HAS_ASM_GOTO_OUTPUT) && defined(CONFIG_CC_HAS_ASM_AOR_FORMAT_FLAGS)
|
2020-09-14 13:42:25 +02:00
|
|
|
|
2025-01-13 13:36:59 +01:00
|
|
|
#define __mvc_kernel_nofault(dst, src, type, err_label) \
|
2020-09-14 13:42:25 +02:00
|
|
|
do { \
|
|
|
|
switch (sizeof(type)) { \
|
|
|
|
case 1: \
|
|
|
|
case 2: \
|
|
|
|
case 4: \
|
|
|
|
case 8: \
|
2025-01-13 13:37:00 +01:00
|
|
|
asm goto( \
|
2025-01-13 13:36:59 +01:00
|
|
|
"0: mvc %O[_dst](%[_len],%R[_dst]),%[_src]\n" \
|
2025-01-13 13:37:00 +01:00
|
|
|
"1: nopr %%r7\n" \
|
|
|
|
EX_TABLE(0b, %l[err_label]) \
|
|
|
|
EX_TABLE(1b, %l[err_label]) \
|
|
|
|
: [_dst] "=Q" (*(type *)dst) \
|
2025-01-13 13:36:59 +01:00
|
|
|
: [_src] "Q" (*(type *)(src)), \
|
2025-01-13 13:37:00 +01:00
|
|
|
[_len] "I" (sizeof(type)) \
|
|
|
|
: \
|
|
|
|
: err_label); \
|
2020-09-14 13:42:25 +02:00
|
|
|
break; \
|
|
|
|
default: \
|
2025-01-13 13:36:59 +01:00
|
|
|
__mvc_kernel_nofault_bad(); \
|
2020-09-14 13:42:25 +02:00
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2025-01-13 13:37:00 +01:00
|
|
|
#else /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT) && CONFIG_CC_HAS_ASM_AOR_FORMAT_FLAGS */
|
2020-09-14 13:42:25 +02:00
|
|
|
|
2025-01-13 13:36:59 +01:00
|
|
|
#define __mvc_kernel_nofault(dst, src, type, err_label) \
|
2020-09-14 13:42:25 +02:00
|
|
|
do { \
|
2025-01-13 13:36:59 +01:00
|
|
|
type *(__dst) = (type *)(dst); \
|
|
|
|
int __rc; \
|
2020-09-14 13:42:25 +02:00
|
|
|
\
|
|
|
|
switch (sizeof(type)) { \
|
2025-01-13 13:36:59 +01:00
|
|
|
case 1: \
|
|
|
|
case 2: \
|
|
|
|
case 4: \
|
|
|
|
case 8: \
|
|
|
|
asm_inline volatile( \
|
|
|
|
"0: mvc 0(%[_len],%[_dst]),%[_src]\n" \
|
|
|
|
"1: lhi %[_rc],0\n" \
|
|
|
|
"2:\n" \
|
|
|
|
EX_TABLE_UA_FAULT(0b, 2b, %[_rc]) \
|
|
|
|
EX_TABLE_UA_FAULT(1b, 2b, %[_rc]) \
|
|
|
|
: [_rc] "=d" (__rc), \
|
|
|
|
"=m" (*__dst) \
|
|
|
|
: [_src] "Q" (*(type *)(src)), \
|
|
|
|
[_dst] "a" (__dst), \
|
|
|
|
[_len] "I" (sizeof(type))); \
|
|
|
|
if (__rc) \
|
|
|
|
goto err_label; \
|
2020-09-14 13:42:25 +02:00
|
|
|
break; \
|
|
|
|
default: \
|
2025-01-13 13:36:59 +01:00
|
|
|
__mvc_kernel_nofault_bad(); \
|
2020-09-14 13:42:25 +02:00
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2025-01-13 13:37:00 +01:00
|
|
|
#endif /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT && CONFIG_CC_HAS_ASM_AOR_FORMAT_FLAGS */
|
2025-01-13 13:36:59 +01:00
|
|
|
|
|
|
|
#define __get_kernel_nofault __mvc_kernel_nofault
|
|
|
|
#define __put_kernel_nofault __mvc_kernel_nofault
|
|
|
|
|
2022-11-02 15:19:23 +01:00
|
|
|
void __cmpxchg_user_key_called_with_bad_pointer(void);
|
|
|
|
|
2025-06-16 17:00:29 +02:00
|
|
|
int __cmpxchg_user_key1(unsigned long address, unsigned char *uval,
|
|
|
|
unsigned char old, unsigned char new, unsigned long key);
|
|
|
|
int __cmpxchg_user_key2(unsigned long address, unsigned short *uval,
|
|
|
|
unsigned short old, unsigned short new, unsigned long key);
|
|
|
|
int __cmpxchg_user_key4(unsigned long address, unsigned int *uval,
|
|
|
|
unsigned int old, unsigned int new, unsigned long key);
|
|
|
|
int __cmpxchg_user_key8(unsigned long address, unsigned long *uval,
|
|
|
|
unsigned long old, unsigned long new, unsigned long key);
|
|
|
|
int __cmpxchg_user_key16(unsigned long address, __uint128_t *uval,
|
|
|
|
__uint128_t old, __uint128_t new, unsigned long key);
|
|
|
|
|
|
|
|
static __always_inline int _cmpxchg_user_key(unsigned long address, void *uval,
|
|
|
|
__uint128_t old, __uint128_t new,
|
|
|
|
unsigned long key, int size)
|
2022-11-02 15:19:23 +01:00
|
|
|
{
|
|
|
|
switch (size) {
|
2025-06-16 17:00:29 +02:00
|
|
|
case 1: return __cmpxchg_user_key1(address, uval, old, new, key);
|
|
|
|
case 2: return __cmpxchg_user_key2(address, uval, old, new, key);
|
|
|
|
case 4: return __cmpxchg_user_key4(address, uval, old, new, key);
|
|
|
|
case 8: return __cmpxchg_user_key8(address, uval, old, new, key);
|
|
|
|
case 16: return __cmpxchg_user_key16(address, uval, old, new, key);
|
|
|
|
default: __cmpxchg_user_key_called_with_bad_pointer();
|
2022-11-02 15:19:23 +01:00
|
|
|
}
|
2025-06-16 17:00:29 +02:00
|
|
|
return 0;
|
2022-11-02 15:19:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* cmpxchg_user_key() - cmpxchg with user space target, honoring storage keys
|
|
|
|
* @ptr: User space address of value to compare to @old and exchange with
|
|
|
|
* @new. Must be aligned to sizeof(*@ptr).
|
|
|
|
* @uval: Address where the old value of *@ptr is written to.
|
|
|
|
* @old: Old value. Compared to the content pointed to by @ptr in order to
|
|
|
|
* determine if the exchange occurs. The old value read from *@ptr is
|
|
|
|
* written to *@uval.
|
|
|
|
* @new: New value to place at *@ptr.
|
|
|
|
* @key: Access key to use for checking storage key protection.
|
|
|
|
*
|
|
|
|
* Perform a cmpxchg on a user space target, honoring storage key protection.
|
|
|
|
* @key alone determines how key checking is performed, neither
|
|
|
|
* storage-protection-override nor fetch-protection-override apply.
|
|
|
|
* The caller must compare *@uval and @old to determine if values have been
|
|
|
|
* exchanged. In case of an exception *@uval is set to zero.
|
|
|
|
*
|
|
|
|
* Return: 0: cmpxchg executed
|
|
|
|
* -EFAULT: an exception happened when trying to access *@ptr
|
2022-11-17 11:07:45 +01:00
|
|
|
* -EAGAIN: maxed out number of retries (byte and short only)
|
2022-11-02 15:19:23 +01:00
|
|
|
*/
|
|
|
|
#define cmpxchg_user_key(ptr, uval, old, new, key) \
|
|
|
|
({ \
|
|
|
|
__typeof__(ptr) __ptr = (ptr); \
|
|
|
|
__typeof__(uval) __uval = (uval); \
|
|
|
|
\
|
|
|
|
BUILD_BUG_ON(sizeof(*(__ptr)) != sizeof(*(__uval))); \
|
|
|
|
might_fault(); \
|
|
|
|
__chk_user_ptr(__ptr); \
|
2025-06-16 17:00:29 +02:00
|
|
|
_cmpxchg_user_key((unsigned long)(__ptr), (void *)(__uval), \
|
|
|
|
(old), (new), (key), sizeof(*(__ptr))); \
|
2022-11-02 15:19:23 +01:00
|
|
|
})
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
#endif /* __S390_UACCESS_H */
|