]> Pileus Git - ~andy/linux/commitdiff
x86: use generic strncpy_from_user routine
authorLinus Torvalds <torvalds@linux-foundation.org>
Sat, 26 May 2012 17:14:39 +0000 (10:14 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 26 May 2012 17:14:39 +0000 (10:14 -0700)
The generic strncpy_from_user() is not really optimal, since it is
designed to work on both little-endian and big-endian.  And on
little-endian you can simplify much of the logic to find the first zero
byte, since little-endian arithmetic doesn't have to worry about the
carry bit propagating into earlier bytes (only later bytes, which we
don't care about).

But I have patches to make the generic routines use the architecture-
specific <asm/word-at-a-time.h> infrastructure, so that we can regain
the little-endian optimizations.  But before we do that, switch over to
the generic routines to make the patches each do just one well-defined
thing.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
arch/x86/Kconfig
arch/x86/include/asm/uaccess.h
arch/x86/lib/usercopy.c

index 81c3e8be789a1fac5ca35dd3cd811c527d33af13..3220d44e24d016ed428648a70cb7dd0de7583c69 100644 (file)
@@ -93,6 +93,7 @@ config X86
        select GENERIC_CLOCKEVENTS_BROADCAST if X86_64 || (X86_32 && X86_LOCAL_APIC)
        select GENERIC_TIME_VSYSCALL if X86_64
        select KTIME_SCALAR if X86_32
+       select GENERIC_STRNCPY_FROM_USER
 
 config INSTRUCTION_DECODER
        def_bool (KPROBES || PERF_EVENTS || UPROBES)
index 851fe0dc13bc18c33c79b8d54deb06e899ae63ad..1354facd8f636cee8a4674e085ae6f5319ef002d 100644 (file)
@@ -32,6 +32,7 @@
 
 #define segment_eq(a, b)       ((a).seg == (b).seg)
 
+#define user_addr_max() (current_thread_info()->addr_limit.seg)
 #define __addr_ok(addr)                                        \
        ((unsigned long __force)(addr) <                \
         (current_thread_info()->addr_limit.seg))
index 2e4e4b02c37a62cd593f3a59645898d946d95ffb..f61ee67ec00f0dc6d61b6165e087ba248f81df2c 100644 (file)
@@ -43,100 +43,3 @@ copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
        return len;
 }
 EXPORT_SYMBOL_GPL(copy_from_user_nmi);
-
-/*
- * Do a strncpy, return length of string without final '\0'.
- * 'count' is the user-supplied count (return 'count' if we
- * hit it), 'max' is the address space maximum (and we return
- * -EFAULT if we hit it).
- */
-static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max)
-{
-       long res = 0;
-
-       /*
-        * Truncate 'max' to the user-specified limit, so that
-        * we only have one limit we need to check in the loop
-        */
-       if (max > count)
-               max = count;
-
-       while (max >= sizeof(unsigned long)) {
-               unsigned long c, mask;
-
-               /* Fall back to byte-at-a-time if we get a page fault */
-               if (unlikely(__get_user(c,(unsigned long __user *)(src+res))))
-                       break;
-               mask = has_zero(c);
-               if (mask) {
-                       mask = (mask - 1) & ~mask;
-                       mask >>= 7;
-                       *(unsigned long *)(dst+res) = c & mask;
-                       return res + count_masked_bytes(mask);
-               }
-               *(unsigned long *)(dst+res) = c;
-               res += sizeof(unsigned long);
-               max -= sizeof(unsigned long);
-       }
-
-       while (max) {
-               char c;
-
-               if (unlikely(__get_user(c,src+res)))
-                       return -EFAULT;
-               dst[res] = c;
-               if (!c)
-                       return res;
-               res++;
-               max--;
-       }
-
-       /*
-        * Uhhuh. We hit 'max'. But was that the user-specified maximum
-        * too? If so, that's ok - we got as much as the user asked for.
-        */
-       if (res >= count)
-               return res;
-
-       /*
-        * Nope: we hit the address space limit, and we still had more
-        * characters the caller would have wanted. That's an EFAULT.
-        */
-       return -EFAULT;
-}
-
-/**
- * strncpy_from_user: - Copy a NUL terminated string from userspace.
- * @dst:   Destination address, in kernel space.  This buffer must be at
- *         least @count bytes long.
- * @src:   Source address, in user space.
- * @count: Maximum number of bytes to copy, including the trailing NUL.
- *
- * Copies a NUL-terminated string from userspace to kernel space.
- *
- * On success, returns the length of the string (not including the trailing
- * NUL).
- *
- * If access to userspace fails, returns -EFAULT (some data may have been
- * copied).
- *
- * If @count is smaller than the length of the string, copies @count bytes
- * and returns @count.
- */
-long
-strncpy_from_user(char *dst, const char __user *src, long count)
-{
-       unsigned long max_addr, src_addr;
-
-       if (unlikely(count <= 0))
-               return 0;
-
-       max_addr = current_thread_info()->addr_limit.seg;
-       src_addr = (unsigned long)src;
-       if (likely(src_addr < max_addr)) {
-               unsigned long max = max_addr - src_addr;
-               return do_strncpy_from_user(dst, src, count, max);
-       }
-       return -EFAULT;
-}
-EXPORT_SYMBOL(strncpy_from_user);