mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "tip-bot for H. Peter Anvin" <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, hpa@zytor.com, mingo@kernel.org,
	torvalds@linux-foundation.org, tglx@linutronix.de
Subject: [tip:x86/asm] x86: Slightly tweak the access_ok() C variant for better code
Date: Fri, 27 Dec 2013 17:00:50 -0800	[thread overview]
Message-ID: <tip-34f873a773c59f738aff8f412fa59d528f1b3e3f@git.kernel.org> (raw)
In-Reply-To: <CA+55aFzPBdbfKovMT8Edr4SmE2_=+OKJFac9XW2awegogTkVTA@mail.gmail.com>

Commit-ID:  34f873a773c59f738aff8f412fa59d528f1b3e3f
Gitweb:     http://git.kernel.org/tip/34f873a773c59f738aff8f412fa59d528f1b3e3f
Author:     H. Peter Anvin <hpa@zytor.com>
AuthorDate: Fri, 27 Dec 2013 16:52:47 -0800
Committer:  H. Peter Anvin <hpa@zytor.com>
CommitDate: Fri, 27 Dec 2013 16:58:17 -0800

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>
---
 arch/x86/include/asm/uaccess.h | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index 84ecf1d..4f02113 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -41,7 +41,7 @@
  * Test whether a block of memory is a valid user space address.
  * Returns 0 if the range is valid, nonzero otherwise.
  */
-static inline int __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
+static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
 {
 	/*
 	 * 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
 
 	/* Arbitrary sizes? Be careful about overflow */
 	addr += size;
-	return (addr < size) || (addr > limit);
+	if (addr < size)
+		return true;
+	return addr > limit;
 }
 
 #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
  * this function, memory access functions may still return -EFAULT.
  */
 #define access_ok(type, addr, size) \
-	(likely(__range_not_ok(addr, size, user_addr_max()) == 0))
+	unlikely(!__range_not_ok(addr, size, user_addr_max()))
 
 /*
  * The exception table consists of pairs of addresses relative to the

  parent reply	other threads:[~2013-12-28  1:01 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-21 20:27 [RFC] speeding up the stat() family of system calls Linus Torvalds
2013-12-21 22:54 ` John Stoffel
2013-12-22  0:11   ` Linus Torvalds
2013-12-24  0:00 ` H. Peter Anvin
2013-12-24  0:12   ` Linus Torvalds
2013-12-24  6:00     ` H. Peter Anvin
2013-12-24 20:46 ` Ingo Molnar
2013-12-26 19:00   ` Linus Torvalds
2013-12-27  0:45     ` H. Peter Anvin
2013-12-27  3:18       ` H. Peter Anvin
2013-12-27  6:09     ` H. Peter Anvin
2013-12-27 23:30       ` H. Peter Anvin
2014-01-12 17:46         ` Ingo Molnar
2013-12-28  1:00     ` [tip:x86/asm] x86: Replace assembly access_ok() with a C variant tip-bot for Linus Torvalds
2013-12-28  1:00     ` tip-bot for H. Peter Anvin [this message]
2013-12-28  1:06     ` [tip:x86/asm] x86: Slightly tweak the access_ok() C variant for better code tip-bot for H. Peter Anvin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tip-34f873a773c59f738aff8f412fa59d528f1b3e3f@git.kernel.org \
    --to=tipbot@zytor.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome