Skip to content

Commit a740576

Browse files
author
H. Peter Anvin
committed
x86: Slightly tweak the access_ok() C variant for better code
gcc can under very specific circumstances realize that the code sequence: foo += bar; if (foo < bar) ... ... is equivalent to a carry out from the addition. Tweak the implementation of access_ok() (specifically __chk_range_not_ok()) to make it more likely that gcc will make that connection. It isn't fool-proof (sometimes gcc seems to think it can make better code with lea, and ends up with a second comparison), still, but it seems to be able to connect the two more frequently this way. Cc: Linus Torvalds <torvalds@linux-foundation.org> Link: http://lkml.kernel.org/r/CA%2B55aFzPBdbfKovMT8Edr4SmE2_=%2BOKJFac9XW2awegogTkVTA@mail.gmail.com Signed-off-by: H. Peter Anvin <hpa@zytor.com>
1 parent c5fe5d8 commit a740576

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

arch/x86/include/asm/uaccess.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* Test whether a block of memory is a valid user space address.
4242
* Returns 0 if the range is valid, nonzero otherwise.
4343
*/
44-
static inline int __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
44+
static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
4545
{
4646
/*
4747
* If we have used "sizeof()" for the size,
@@ -55,7 +55,9 @@ static inline int __chk_range_not_ok(unsigned long addr, unsigned long size, uns
5555

5656
/* Arbitrary sizes? Be careful about overflow */
5757
addr += size;
58-
return (addr < size) || (addr > limit);
58+
if (addr < size)
59+
return true;
60+
return addr > limit;
5961
}
6062

6163
#define __range_not_ok(addr, size, limit) \
@@ -84,7 +86,7 @@ static inline int __chk_range_not_ok(unsigned long addr, unsigned long size, uns
8486
* this function, memory access functions may still return -EFAULT.
8587
*/
8688
#define access_ok(type, addr, size) \
87-
(likely(__range_not_ok(addr, size, user_addr_max()) == 0))
89+
likely(!__range_not_ok(addr, size, user_addr_max()))
8890

8991
/*
9092
* The exception table consists of pairs of addresses relative to the

0 commit comments

Comments
 (0)