Skip to content

Commit 12a0ef7

Browse files
wildea01ctmarinas
authored andcommitted
arm64: use generic strnlen_user and strncpy_from_user functions
This patch implements the word-at-a-time interface for arm64 using the same algorithm as ARM. We use the fls64 macro, which expands to a clz instruction via a compiler builtin. Big-endian configurations make use of the implementation from asm-generic. With this implemented, we can replace our byte-at-a-time strnlen_user and strncpy_from_user functions with the optimised generic versions. Signed-off-by: Will Deacon <will.deacon@arm.com> Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent 7158627 commit 12a0ef7

7 files changed

Lines changed: 64 additions & 127 deletions

File tree

‎arch/arm64/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ config ARM64
1717
select GENERIC_IRQ_SHOW
1818
select GENERIC_SCHED_CLOCK
1919
select GENERIC_SMP_IDLE_THREAD
20+
select GENERIC_STRNCPY_FROM_USER
21+
select GENERIC_STRNLEN_USER
2022
select GENERIC_TIME_VSYSCALL
2123
select HARDIRQS_SW_RESEND
2224
select HAVE_ARCH_TRACEHOOK

‎arch/arm64/include/asm/uaccess.h‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ static inline void set_fs(mm_segment_t fs)
100100
})
101101

102102
#define access_ok(type, addr, size) __range_ok(addr, size)
103+
#define user_addr_max get_fs
103104

104105
/*
105106
* The "__xxx" versions of the user access functions do not verify the address
@@ -240,9 +241,6 @@ extern unsigned long __must_check __copy_to_user(void __user *to, const void *fr
240241
extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
241242
extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
242243

243-
extern unsigned long __must_check __strncpy_from_user(char *to, const char __user *from, unsigned long count);
244-
extern unsigned long __must_check __strnlen_user(const char __user *s, long n);
245-
246244
static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
247245
{
248246
if (access_ok(VERIFY_READ, from, n))
@@ -276,24 +274,9 @@ static inline unsigned long __must_check clear_user(void __user *to, unsigned lo
276274
return n;
277275
}
278276

279-
static inline long __must_check strncpy_from_user(char *dst, const char __user *src, long count)
280-
{
281-
long res = -EFAULT;
282-
if (access_ok(VERIFY_READ, src, 1))
283-
res = __strncpy_from_user(dst, src, count);
284-
return res;
285-
}
286-
287-
#define strlen_user(s) strnlen_user(s, ~0UL >> 1)
277+
extern long strncpy_from_user(char *dest, const char __user *src, long count);
288278

289-
static inline long __must_check strnlen_user(const char __user *s, long n)
290-
{
291-
unsigned long res = 0;
292-
293-
if (__addr_ok(s))
294-
res = __strnlen_user(s, n);
295-
296-
return res;
297-
}
279+
extern __must_check long strlen_user(const char __user *str);
280+
extern __must_check long strnlen_user(const char __user *str, long n);
298281

299282
#endif /* __ASM_UACCESS_H */
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (C) 2013 ARM Ltd.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 as
6+
* published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
#ifndef __ASM_WORD_AT_A_TIME_H
17+
#define __ASM_WORD_AT_A_TIME_H
18+
19+
#ifndef __AARCH64EB__
20+
21+
#include <linux/kernel.h>
22+
23+
struct word_at_a_time {
24+
const unsigned long one_bits, high_bits;
25+
};
26+
27+
#define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
28+
29+
static inline unsigned long has_zero(unsigned long a, unsigned long *bits,
30+
const struct word_at_a_time *c)
31+
{
32+
unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
33+
*bits = mask;
34+
return mask;
35+
}
36+
37+
#define prep_zero_mask(a, bits, c) (bits)
38+
39+
static inline unsigned long create_zero_mask(unsigned long bits)
40+
{
41+
bits = (bits - 1) & ~bits;
42+
return bits >> 7;
43+
}
44+
45+
static inline unsigned long find_zero(unsigned long mask)
46+
{
47+
return fls64(mask) >> 3;
48+
}
49+
50+
#else /* __AARCH64EB__ */
51+
#include <asm-generic/word-at-a-time.h>
52+
#endif
53+
54+
#endif /* __ASM_WORD_AT_A_TIME_H */

‎arch/arm64/kernel/arm64ksyms.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@
2929

3030
#include <asm/checksum.h>
3131

32-
/* user mem (segment) */
33-
EXPORT_SYMBOL(__strnlen_user);
34-
EXPORT_SYMBOL(__strncpy_from_user);
35-
3632
EXPORT_SYMBOL(copy_page);
3733
EXPORT_SYMBOL(clear_page);
3834

35+
/* user mem (segment) */
3936
EXPORT_SYMBOL(__copy_from_user);
4037
EXPORT_SYMBOL(__copy_to_user);
4138
EXPORT_SYMBOL(__clear_user);

‎arch/arm64/lib/Makefile‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
lib-y := bitops.o delay.o \
2-
strncpy_from_user.o strnlen_user.o clear_user.o \
3-
copy_from_user.o copy_to_user.o copy_in_user.o \
4-
copy_page.o clear_page.o \
5-
memchr.o memcpy.o memmove.o memset.o \
1+
lib-y := bitops.o clear_user.o delay.o copy_from_user.o \
2+
copy_to_user.o copy_in_user.o copy_page.o \
3+
clear_page.o memchr.o memcpy.o memmove.o memset.o \
64
strchr.o strrchr.o

‎arch/arm64/lib/strncpy_from_user.S‎

Lines changed: 0 additions & 50 deletions
This file was deleted.

‎arch/arm64/lib/strnlen_user.S‎

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)