mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vitaly Mayatskikh <v.mayatskih@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Andi Kleen <andi@firstfloor.org>, Ingo Molnar <mingo@elte.hu>
Subject: [PATCH] x86: Optimize tail handling for copy_user
Date: Mon, 28 Jul 2008 16:10:30 +0200	[thread overview]
Message-ID: <m3ljzmdrqh.fsf@gravicappa.englab.brq.redhat.com> (raw)

Reduce protection faults count in copy_user_handle_tail routine by
limiting clear length to the end of page as was suggested by Linus.

Linus, should I add myself "signed-off-by: you" for patches with your
ideas implemented? Clear length calculation was changed a bit for
correct handling of page aligned addresses.

I'm using this systemtap script for sanity testing:
http://people.redhat.com/vmayatsk/copy_user_x8664/
It looks very ugly, but works.

Signed-off-by: Vitaly Mayatskikh <v.mayatskih@gmail.com>

diff --git a/arch/x86/lib/usercopy_64.c b/arch/x86/lib/usercopy_64.c
index f4df6e7..42baeca 100644
--- a/arch/x86/lib/usercopy_64.c
+++ b/arch/x86/lib/usercopy_64.c
@@ -161,23 +161,33 @@ EXPORT_SYMBOL(copy_in_user);
 /*
  * Try to copy last bytes and clear the rest if needed.
  * Since protection fault in copy_from/to_user is not a normal situation,
- * it is not necessary to optimize tail handling.
+ * it is not necessary to do low level optimization of tail handling.
  */
 unsigned long
-copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest)
+copy_user_handle_tail(char *to, char *from, unsigned len, unsigned clear_rest)
 {
 	char c;
-	unsigned zero_len;
+	unsigned clear_len;
 
-	for (; len; --len) {
-		if (__get_user_nocheck(c, from++, sizeof(char)))
-			break;
-		if (__put_user_nocheck(c, to++, sizeof(char)))
+	while (len) {
+		if (__get_user_nocheck(c, from, 1))
 			break;
+		from++;
+		if (__put_user_nocheck(c, to, 1))
+			/* Fault in destination, nothing to clear */
+			goto out;
+		to++;
+		len--;
 	}
 
-	for (c = 0, zero_len = len; zerorest && zero_len; --zero_len)
-		if (__put_user_nocheck(c, to++, sizeof(char)))
-			break;
+	if (clear_rest) {
+		unsigned long addr = (unsigned long)to;
+		clear_len = len;
+		/* Limit clear_len to the rest of the page */
+		if ((addr + len) & PAGE_MASK != addr & PAGE_MASK)
+			clear_len = len - ((addr + len) & ~PAGE_MASK);
+		memset(to, 0, clear_len);
+	}
+out:
 	return len;
 }
diff --git a/include/asm-x86/uaccess_64.h b/include/asm-x86/uaccess_64.h
index 5cfd295..c5c5af0 100644
--- a/include/asm-x86/uaccess_64.h
+++ b/include/asm-x86/uaccess_64.h
@@ -196,6 +196,6 @@ static inline int __copy_from_user_inatomic_nocache(void *dst,
 }
 
 unsigned long
-copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest);
+copy_user_handle_tail(char *to, char *from, unsigned len, unsigned clear_rest);
 
 #endif /* ASM_X86__UACCESS_64_H */

-- 
wbr, Vitaly

             reply	other threads:[~2008-07-28 14:10 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-28 14:10 Vitaly Mayatskikh [this message]
2008-07-28 15:48 ` Linus Torvalds
2008-07-29 15:24   ` Vitaly Mayatskikh
2008-07-30 12:02   ` Vitaly Mayatskikh
2008-07-30 17:29     ` Linus Torvalds
2008-07-31 14:27       ` Vitaly Mayatskikh
2008-08-01 20:10         ` Linus Torvalds
2008-08-01 22:04           ` Vitaly Mayatskikh
2008-08-01 22:30             ` Linus Torvalds
2008-08-01 22:53               ` Vitaly Mayatskikh
2008-07-30 12:07   ` Vitaly Mayatskikh
2008-07-30 12:13   ` Vitaly Mayatskikh

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=m3ljzmdrqh.fsf@gravicappa.englab.brq.redhat.com \
    --to=v.mayatskih@gmail.com \
    --cc=andi@firstfloor.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --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

all inboxes | Powered by JetHome®