#include <linux/errno.h>
 #include <linux/bug.h>
 
+#include <asm/byteorder.h>
+
 void copy_from_user_overflow(void)
 {
        WARN(1, "Buffer overflow detected!\n");
 static inline long find_zero(unsigned long mask)
 {
        long byte = 0;
+
+#ifdef __BIG_ENDIAN
 #ifdef CONFIG_64BIT
        if (mask >> 32)
                mask >>= 32;
        else
                byte += 2;
        return (mask >> 8) ? byte : byte + 1;
+#else
+#ifdef CONFIG_64BIT
+       if (!((unsigned int) mask)) {
+               mask >>= 32;
+               byte = 4;
+       }
+#endif
+       if (!(mask & 0xffff)) {
+               mask >>= 16;
+               byte += 2;
+       }
+       return (mask & 0xff) ? byte : byte + 1;
+#endif
 }
 
+#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
+#define IS_UNALIGNED(src, dst) 0
+#else
+#define IS_UNALIGNED(src, dst) \
+       (((long) dst | (long) src) & (sizeof(long) - 1))
+#endif
+
 /*
  * Do a strncpy, return length of string without final '\0'.
  * 'count' is the user-supplied count (return 'count' if we
        if (max > count)
                max = count;
 
-       if (((long) dst | (long) src) & (sizeof(long) - 1))
+       if (IS_UNALIGNED(src, dst))
                goto byte_at_a_time;
 
        while (max >= sizeof(unsigned long)) {
                v = (c + high_bits) & ~rhs;
                *(unsigned long *)(dst+res) = c;
                if (v) {
-                       v = (c & low_bits) + low_bits;;
+                       v = (c & low_bits) + low_bits;
                        v = ~(v | rhs);
                        return res + find_zero(v);
                }
        if (unlikely(count <= 0))
                return 0;
 
-       max_addr = ~0UL;
-       if (likely(segment_eq(get_fs(), USER_DS)))
-               max_addr = STACK_TOP;
+       max_addr = user_addr_max();
        src_addr = (unsigned long)src;
        if (likely(src_addr < max_addr)) {
                unsigned long max = max_addr - src_addr;