Skip to content

Commit 8c56cc8

Browse files
wildea01Russell King
authored andcommitted
ARM: 7449/1: use generic strnlen_user and strncpy_from_user functions
This patch implements the word-at-a-time interface for ARM using the same algorithm as x86. We use the fls macro from ARMv5 onwards, where we have a clz instruction available which saves us a mov instruction when targetting Thumb-2. For older CPUs, we use the magic 0x0ff0001 constant. 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. Reviewed-by: Nicolas Pitre <nico@linaro.org> Signed-off-by: Will Deacon <will.deacon@arm.com> Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
1 parent 4295b89 commit 8c56cc8

7 files changed

Lines changed: 63 additions & 109 deletions

File tree

‎arch/arm/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ config ARM
4646
select GENERIC_SMP_IDLE_THREAD
4747
select KTIME_SCALAR
4848
select GENERIC_CLOCKEVENTS_BROADCAST if SMP
49+
select GENERIC_STRNCPY_FROM_USER
50+
select GENERIC_STRNLEN_USER
4951
help
5052
The ARM series is a line of low-power-consumption RISC chip designs
5153
licensed by ARM Ltd and targeted at embedded applications and

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ static inline void set_fs(mm_segment_t fs)
189189

190190
#define access_ok(type,addr,size) (__range_ok(addr,size) == 0)
191191

192+
#define user_addr_max() \
193+
(segment_eq(get_fs(), USER_DS) ? TASK_SIZE : ~0UL)
194+
192195
/*
193196
* The "__xxx" versions of the user access functions do not verify the
194197
* address space - it must have been done previously with a separate
@@ -398,9 +401,6 @@ extern unsigned long __must_check __clear_user_std(void __user *addr, unsigned l
398401
#define __clear_user(addr,n) (memset((void __force *)addr, 0, n), 0)
399402
#endif
400403

401-
extern unsigned long __must_check __strncpy_from_user(char *to, const char __user *from, unsigned long count);
402-
extern unsigned long __must_check __strnlen_user(const char __user *s, long n);
403-
404404
static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
405405
{
406406
if (access_ok(VERIFY_READ, from, n))
@@ -427,24 +427,9 @@ static inline unsigned long __must_check clear_user(void __user *to, unsigned lo
427427
return n;
428428
}
429429

430-
static inline long __must_check strncpy_from_user(char *dst, const char __user *src, long count)
431-
{
432-
long res = -EFAULT;
433-
if (access_ok(VERIFY_READ, src, 1))
434-
res = __strncpy_from_user(dst, src, count);
435-
return res;
436-
}
437-
438-
#define strlen_user(s) strnlen_user(s, ~0UL >> 1)
430+
extern long strncpy_from_user(char *dest, const char __user *src, long count);
439431

440-
static inline long __must_check strnlen_user(const char __user *s, long n)
441-
{
442-
unsigned long res = 0;
443-
444-
if (__addr_ok(s))
445-
res = __strnlen_user(s, n);
446-
447-
return res;
448-
}
432+
extern __must_check long strlen_user(const char __user *str);
433+
extern __must_check long strnlen_user(const char __user *str, long n);
449434

450435
#endif /* _ASMARM_UACCESS_H */
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef __ASM_ARM_WORD_AT_A_TIME_H
2+
#define __ASM_ARM_WORD_AT_A_TIME_H
3+
4+
#ifndef __ARMEB__
5+
6+
/*
7+
* Little-endian word-at-a-time zero byte handling.
8+
* Heavily based on the x86 algorithm.
9+
*/
10+
#include <linux/kernel.h>
11+
12+
struct word_at_a_time {
13+
const unsigned long one_bits, high_bits;
14+
};
15+
16+
#define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
17+
18+
static inline unsigned long has_zero(unsigned long a, unsigned long *bits,
19+
const struct word_at_a_time *c)
20+
{
21+
unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
22+
*bits = mask;
23+
return mask;
24+
}
25+
26+
#define prep_zero_mask(a, bits, c) (bits)
27+
28+
static inline unsigned long create_zero_mask(unsigned long bits)
29+
{
30+
bits = (bits - 1) & ~bits;
31+
return bits >> 7;
32+
}
33+
34+
static inline unsigned long find_zero(unsigned long mask)
35+
{
36+
unsigned long ret;
37+
38+
#if __LINUX_ARM_ARCH__ >= 5
39+
/* We have clz available. */
40+
ret = fls(mask) >> 3;
41+
#else
42+
/* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
43+
ret = (0x0ff0001 + mask) >> 23;
44+
/* Fix the 1 for 00 case */
45+
ret &= mask;
46+
#endif
47+
48+
return ret;
49+
}
50+
51+
#else /* __ARMEB__ */
52+
#include <asm-generic/word-at-a-time.h>
53+
#endif
54+
55+
#endif /* __ASM_ARM_WORD_AT_A_TIME_H */

‎arch/arm/kernel/armksyms.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ EXPORT_SYMBOL(memmove);
8787
EXPORT_SYMBOL(memchr);
8888
EXPORT_SYMBOL(__memzero);
8989

90-
/* user mem (segment) */
91-
EXPORT_SYMBOL(__strnlen_user);
92-
EXPORT_SYMBOL(__strncpy_from_user);
93-
9490
#ifdef CONFIG_MMU
9591
EXPORT_SYMBOL(copy_page);
9692

‎arch/arm/lib/Makefile‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ lib-y := backtrace.o changebit.o csumipv6.o csumpartial.o \
88
csumpartialcopy.o csumpartialcopyuser.o clearbit.o \
99
delay.o findbit.o memchr.o memcpy.o \
1010
memmove.o memset.o memzero.o setbit.o \
11-
strncpy_from_user.o strnlen_user.o \
1211
strchr.o strrchr.o \
1312
testchangebit.o testclearbit.o testsetbit.o \
1413
ashldi3.o ashrdi3.o lshrdi3.o muldi3.o \

‎arch/arm/lib/strncpy_from_user.S‎

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

‎arch/arm/lib/strnlen_user.S‎

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

0 commit comments

Comments
 (0)