---

 25-akpm/include/asm-alpha/unistd.h     |    4 ++
 25-akpm/include/asm-arm/unistd.h       |    4 ++
 25-akpm/include/asm-arm26/unistd.h     |    4 ++
 25-akpm/include/asm-h8300/unistd.h     |    4 ++
 25-akpm/include/asm-i386/unistd.h      |    4 ++
 25-akpm/include/asm-m68k/unistd.h      |    4 ++
 25-akpm/include/asm-m68knommu/unistd.h |   45 ++++++---------------------
 25-akpm/include/asm-mips/unistd.h      |   55 +++++++++++----------------------
 25-akpm/include/asm-parisc/unistd.h    |   17 +++++++---
 25-akpm/include/asm-ppc/unistd.h       |   19 ++++++++---
 25-akpm/include/asm-ppc64/unistd.h     |   19 ++++++++---
 25-akpm/include/asm-s390/unistd.h      |    4 ++
 25-akpm/include/asm-sh/unistd.h        |    4 ++
 25-akpm/include/asm-sparc/unistd.h     |   40 +++++++++---------------
 25-akpm/include/asm-sparc64/unistd.h   |   40 +++++++++---------------
 25-akpm/include/asm-v850/unistd.h      |    5 ++-
 25-akpm/include/asm-x86_64/unistd.h    |    4 ++
 17 files changed, 142 insertions(+), 134 deletions(-)

diff -puN include/asm-alpha/unistd.h~kernel-syscalls-retval-fix include/asm-alpha/unistd.h
--- 25/include/asm-alpha/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.534493080 -0700
+++ 25-akpm/include/asm-alpha/unistd.h	2004-05-02 22:27:36.559489280 -0700
@@ -378,8 +378,12 @@
 
 #if defined(__GNUC__)
 
+#ifdef __KERNEL__
+#define _syscall_return(type)	return ((type) _sc_ret)
+#else
 #define _syscall_return(type)						\
 	return (_sc_err ? errno = _sc_ret, _sc_ret = -1L : 0), (type) _sc_ret
+#endif
 
 #define _syscall_clobbers						\
 	"$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8",			\
diff -puN include/asm-arm26/unistd.h~kernel-syscalls-retval-fix include/asm-arm26/unistd.h
--- 25/include/asm-arm26/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.535492928 -0700
+++ 25-akpm/include/asm-arm26/unistd.h	2004-05-02 22:27:36.560489128 -0700
@@ -277,6 +277,9 @@
 #define __syscall(name) "swi\t" __sys1(__NR_##name) "\n\t"
 #endif
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
 #define __syscall_return(type, res)					\
 do {									\
 	if ((unsigned long)(res) >= (unsigned long)(-125)) {		\
@@ -285,6 +288,7 @@ do {									\
 	}								\
 	return (type) (res);						\
 } while (0)
+#endif
 
 #define _syscall0(type,name)						\
 type name(void) {							\
diff -puN include/asm-arm/unistd.h~kernel-syscalls-retval-fix include/asm-arm/unistd.h
--- 25/include/asm-arm/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.536492776 -0700
+++ 25-akpm/include/asm-arm/unistd.h	2004-05-02 22:27:36.561488976 -0700
@@ -324,6 +324,9 @@
 #endif
 #endif
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
 #define __syscall_return(type, res)					\
 do {									\
 	if ((unsigned long)(res) >= (unsigned long)(-125)) {		\
@@ -332,6 +335,7 @@ do {									\
 	}								\
 	return (type) (res);						\
 } while (0)
+#endif
 
 #define _syscall0(type,name)						\
 type name(void) {							\
diff -puN include/asm-h8300/unistd.h~kernel-syscalls-retval-fix include/asm-h8300/unistd.h
--- 25/include/asm-h8300/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.538492472 -0700
+++ 25-akpm/include/asm-h8300/unistd.h	2004-05-02 22:27:36.561488976 -0700
@@ -276,6 +276,9 @@
 /* user-visible error numbers are in the range -1 - -122: see
    <asm-m68k/errno.h> */
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
 #define __syscall_return(type, res) \
 do { \
 	if ((unsigned long)(res) >= (unsigned long)(-125)) { \
@@ -287,6 +290,7 @@ do { \
 	} \
 	return (type) (res); \
 } while (0)
+#endif
 
 #define _syscall0(type, name)							\
 type name(void)									\
diff -puN include/asm-i386/unistd.h~kernel-syscalls-retval-fix include/asm-i386/unistd.h
--- 25/include/asm-i386/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.540492168 -0700
+++ 25-akpm/include/asm-i386/unistd.h	2004-05-02 22:27:36.562488824 -0700
@@ -293,6 +293,9 @@
 
 /* user-visible error numbers are in the range -1 - -124: see <asm-i386/errno.h> */
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
 #define __syscall_return(type, res) \
 do { \
 	if ((unsigned long)(res) >= (unsigned long)(-125)) { \
@@ -301,6 +304,7 @@ do { \
 	} \
 	return (type) (res); \
 } while (0)
+#endif
 
 /* XXX - _foo needs to be __foo, while __NR_bar could be _NR_bar. */
 #define _syscall0(type,name) \
diff -puN include/asm-m68knommu/unistd.h~kernel-syscalls-retval-fix include/asm-m68knommu/unistd.h
--- 25/include/asm-m68knommu/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.541492016 -0700
+++ 25-akpm/include/asm-m68knommu/unistd.h	2004-05-02 22:27:36.563488672 -0700
@@ -226,17 +226,18 @@
 /* user-visible error numbers are in the range -1 - -122: see
    <asm-m68k/errno.h> */
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
 #define __syscall_return(type, res) \
 do { \
 	if ((unsigned long)(res) >= (unsigned long)(-125)) { \
-	/* avoid using res which is declared to be in register d0; \
-	   errno might expand to a function call and clobber it.  */ \
-		int __err = -(res); \
-		errno = __err; \
+		errno = -(res); \
 		res = -1; \
 	} \
 	return (type) (res); \
 } while (0)
+#endif
 
 #define _syscall0(type, name)							\
 type name(void)									\
@@ -248,11 +249,7 @@ type name(void)									\
 			: "=g" (__res)						\
 			: "i" (__NR_##name)					\
 			: "cc", "%d0");						\
-  if ((unsigned long)(__res) >= (unsigned long)(-125)) {				\
-    errno = -__res;								\
-    __res = -1;									\
-  }										\
-  return (type)__res;								\
+  __syscall_return(type, __res);						\
 }
 
 #define _syscall1(type, name, atype, a)						\
@@ -267,11 +264,7 @@ type name(atype a)								\
 			: "i" (__NR_##name),					\
 			  "g" ((long)a)						\
 			: "cc", "%d0", "%d1");					\
-  if ((unsigned long)(__res) >= (unsigned long)(-125)) {				\
-    errno = -__res;								\
-    __res = -1;									\
-  }										\
-  return (type)__res;								\
+  __syscall_return(type, __res);						\
 }
 
 #define _syscall2(type, name, atype, a, btype, b)				\
@@ -288,11 +281,7 @@ type name(atype a, btype b)							\
 			  "a" ((long)a),					\
 			  "g" ((long)b)						\
 			: "cc", "%d0", "%d1", "%d2");				\
-  if ((unsigned long)(__res) >= (unsigned long)(-125)) {				\
-    errno = -__res;								\
-    __res = -1;									\
-  }										\
-  return (type)__res;								\
+  __syscall_return(type, __res);						\
 }
 
 #define _syscall3(type, name, atype, a, btype, b, ctype, c)			\
@@ -311,11 +300,7 @@ type name(atype a, btype b, ctype c)				
 			  "a" ((long)b),					\
 			  "g" ((long)c)						\
 			: "cc", "%d0", "%d1", "%d2", "%d3");			\
-  if ((unsigned long)(__res) >= (unsigned long)(-125)) {				\
-    errno = -__res;								\
-    __res = -1;									\
-  }										\
-  return (type)__res;								\
+  __syscall_return(type, __res);						\
 }
 
 #define _syscall4(type, name, atype, a, btype, b, ctype, c, dtype, d)		\
@@ -337,11 +322,7 @@ type name(atype a, btype b, ctype c, dty
 			  "g" ((long)d)						\
 			: "cc", "%d0", "%d1", "%d2", "%d3",			\
 			  "%d4");						\
-  if ((unsigned long)(__res) >= (unsigned long)(-125)) {				\
-    errno = -__res;								\
-    __res = -1;									\
-  }										\
-  return (type)__res;								\
+  __syscall_return(type, __res);						\
 }
 
 #define _syscall5(type, name, atype, a, btype, b, ctype, c, dtype, d, etype, e)	\
@@ -365,11 +346,7 @@ type name(atype a, btype b, ctype c, dty
 			  "g" ((long)e)						\
 			: "cc", "%d0", "%d1", "%d2", "%d3",			\
 			  "%d4", "%d5");					\
-  if ((unsigned long)(__res) >= (unsigned long)(-125)) {				\
-    errno = -__res;								\
-    __res = -1;									\
-  }										\
-  return (type)__res;								\
+  __syscall_return(type, __res);						\
 }
 
 
diff -puN include/asm-m68k/unistd.h~kernel-syscalls-retval-fix include/asm-m68k/unistd.h
--- 25/include/asm-m68k/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.543491712 -0700
+++ 25-akpm/include/asm-m68k/unistd.h	2004-05-02 22:27:36.563488672 -0700
@@ -244,6 +244,9 @@
 /* user-visible error numbers are in the range -1 - -124: see
    <asm-m68k/errno.h> */
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
 #define __syscall_return(type, res) \
 do { \
 	if ((unsigned long)(res) >= (unsigned long)(-125)) { \
@@ -255,6 +258,7 @@ do { \
 	} \
 	return (type) (res); \
 } while (0)
+#endif
 
 #define _syscall0(type,name) \
 type name(void) \
diff -puN include/asm-mips/unistd.h~kernel-syscalls-retval-fix include/asm-mips/unistd.h
--- 25/include/asm-mips/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.544491560 -0700
+++ 25-akpm/include/asm-mips/unistd.h	2004-05-02 22:27:36.565488368 -0700
@@ -820,6 +820,16 @@
 
 #ifndef __ASSEMBLY__
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
+do { \
+	if (__a3 == 0) \
+		return (type)(res); \
+	errno = res; \
+	return -1; \
+} while (0)
+
 /* XXX - _foo needs to be __foo, while __NR_bar could be _NR_bar. */
 #define _syscall0(type,name) \
 type name(void) \
@@ -837,10 +847,7 @@ type name(void) \
 	: "i" (__NR_##name) \
 	: "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24"); \
 	\
-	if (__a3 == 0) \
-		return (type) __v0; \
-	errno = __v0; \
-	return -1; \
+	__syscall_return(type, __v0) \
 }
 
 /*
@@ -864,10 +871,7 @@ type name(atype a) \
 	: "r" (__a0), "i" (__NR_##name) \
 	: "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24"); \
 	\
-	if (__a3 == 0) \
-		return (type) __v0; \
-	errno = __v0; \
-	return -1; \
+	__syscall_return(type, __v0) \
 }
 
 #define _syscall2(type,name,atype,a,btype,b) \
@@ -888,10 +892,7 @@ type name(atype a, btype b) \
 	: "r" (__a0), "r" (__a1), "i" (__NR_##name) \
 	: "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24"); \
 	\
-	if (__a3 == 0) \
-		return (type) __v0; \
-	errno = __v0; \
-	return -1; \
+	__syscall_return(type, __v0) \
 }
 
 #define _syscall3(type,name,atype,a,btype,b,ctype,c) \
@@ -913,10 +914,7 @@ type name(atype a, btype b, ctype c) \
 	: "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_##name) \
 	: "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24"); \
 	\
-	if (__a3 == 0) \
-		return (type) __v0; \
-	errno = __v0; \
-	return -1; \
+	__syscall_return(type, __v0) \
 }
 
 #define _syscall4(type,name,atype,a,btype,b,ctype,c,dtype,d) \
@@ -938,10 +936,7 @@ type name(atype a, btype b, ctype c, dty
 	: "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_##name) \
 	: "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24"); \
 	\
-	if (__a3 == 0) \
-		return (type) __v0; \
-	errno = __v0; \
-	return -1; \
+	__syscall_return(type, __v0) \
 }
 
 #if (_MIPS_SIM == _MIPS_SIM_ABI32)
@@ -974,10 +969,7 @@ type name(atype a, btype b, ctype c, dty
 	  "m" ((unsigned long)e) \
 	: "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24"); \
 	\
-	if (__a3 == 0) \
-		return (type) __v0; \
-	errno = __v0; \
-	return -1; \
+	__syscall_return(type, __v0) \
 }
 
 #define _syscall6(type,name,atype,a,btype,b,ctype,c,dtype,d,etype,e,ftype,f) \
@@ -1006,10 +998,7 @@ type name(atype a, btype b, ctype c, dty
 	  "m" ((unsigned long)e), "m" ((unsigned long)f) \
 	: "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24"); \
 	\
-	if (__a3 == 0) \
-		return (type) __v0; \
-	errno = __v0; \
-	return -1; \
+	__syscall_return(type, __v0) \
 }
 
 #endif /* (_MIPS_SIM == _MIPS_SIM_ABI32) */
@@ -1036,10 +1025,7 @@ type name (atype a,btype b,ctype c,dtype
 	: "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_##name) \
 	: "$2","$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24"); \
 	\
-	if (__a3 == 0) \
-		return (type) __v0; \
-	errno = __v0; \
-	return -1; \
+	__syscall_return(type, __v0) \
 }
 
 #define _syscall6(type,name,atype,a,btype,b,ctype,c,dtype,d,etype,e,ftype,f) \
@@ -1064,10 +1050,7 @@ type name (atype a,btype b,ctype c,dtype
 	  "i" (__NR_##name) \
 	: "$2","$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24"); \
 	\
-	if (__a3 == 0) \
-		return (type) __v0; \
-	errno = __v0; \
-	return -1; \
+	__syscall_return(type, __v0) \
 }
 
 #endif /* (_MIPS_SIM == _MIPS_SIM_NABI32) || (_MIPS_SIM == _MIPS_SIM_ABI64) */
diff -puN include/asm-parisc/unistd.h~kernel-syscalls-retval-fix include/asm-parisc/unistd.h
--- 25/include/asm-parisc/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.545491408 -0700
+++ 25-akpm/include/asm-parisc/unistd.h	2004-05-02 22:27:36.566488216 -0700
@@ -739,6 +739,17 @@
 
 #define SYS_ify(syscall_name)   __NR_##syscall_name
 
+#ifdef __KERNEL__
+#define __syscall_return __sys_res
+#else
+#define __syscall_return					\
+        if (__sys_res >= (unsigned long)-4095) {		\
+		errno = -__sys_res;				\
+                __sys_res = (unsigned long)-1;			\
+        }							\
+        __sys_res;						\
+#endif
+
 /* The system call number MUST ALWAYS be loaded in the delay slot of
    the ble instruction, or restarting system calls WILL NOT WORK.  See
    arch/parisc/kernel/signal.c - dhd, 2000-07-26 */
@@ -755,11 +766,7 @@
 			  );                                    \
                 __sys_res = __res;                              \
         }                                                       \
-        if (__sys_res >= (unsigned long)-4095) {                \
-		errno = -__sys_res;				\
-                __sys_res = (unsigned long)-1;                 \
-        }                                                       \
-        __sys_res;                                              \
+	__syscall_return					\
 })
 
 #define K_LOAD_ARGS_0()
diff -puN include/asm-ppc64/unistd.h~kernel-syscalls-retval-fix include/asm-ppc64/unistd.h
--- 25/include/asm-ppc64/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.547491104 -0700
+++ 25-akpm/include/asm-ppc64/unistd.h	2004-05-02 22:27:36.567488064 -0700
@@ -286,6 +286,19 @@
 
 #ifndef __ASSEMBLY__
 
+#ifdef __KERNEL__
+#define __syscall_return						\
+	if (__sc_err & 0x10000000)					\
+		__sc_ret = - __sc_ret;
+#else
+#define __syscall_return						\
+	if (__sc_err & 0x10000000)					\
+	{								\
+		errno = __sc_ret;					\
+		__sc_ret = -1;						\
+	}
+#endif
+
 /* On powerpc a system call basically clobbers the same registers like a
  * function call, with the exception of LR (which is needed for the
  * "sc; bnslr" sequence) and CR (where only CR0.SO is clobbered to signal
@@ -317,11 +330,7 @@
 		__sc_ret = __sc_3;					\
 		__sc_err = __sc_0;					\
 	}								\
-	if (__sc_err & 0x10000000)					\
-	{								\
-		errno = __sc_ret;					\
-		__sc_ret = -1;						\
-	}								\
+	__syscall_return						\
 	return (type) __sc_ret
 
 #define __sc_loadargs_0(name, dummy...)					\
diff -puN include/asm-ppc/unistd.h~kernel-syscalls-retval-fix include/asm-ppc/unistd.h
--- 25/include/asm-ppc/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.548490952 -0700
+++ 25-akpm/include/asm-ppc/unistd.h	2004-05-02 22:27:36.567488064 -0700
@@ -277,6 +277,19 @@
 
 #define __NR(n)	#n
 
+#ifdef __KERNEL__
+#define __syscall_return						\
+	if (__sc_err & 0x10000000)					\
+		__sc_ret = - __sc_ret;
+#else
+#define __syscall_return						\
+	if (__sc_err & 0x10000000)					\
+	{								\
+		errno = __sc_ret;					\
+		__sc_ret = -1;						\
+	}
+#endif
+
 /* On powerpc a system call basically clobbers the same registers like a
  * function call, with the exception of LR (which is needed for the
  * "sc; bnslr" sequence) and CR (where only CR0.SO is clobbered to signal
@@ -307,11 +320,7 @@
 		__sc_ret = __sc_3;					\
 		__sc_err = __sc_0;					\
 	}								\
-	if (__sc_err & 0x10000000)					\
-	{								\
-		errno = __sc_ret;					\
-		__sc_ret = -1;						\
-	}								\
+	__syscall_return						\
 	return (type) __sc_ret
 
 #define __sc_loadargs_0(name, dummy...)					\
diff -puN include/asm-s390/unistd.h~kernel-syscalls-retval-fix include/asm-s390/unistd.h
--- 25/include/asm-s390/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.549490800 -0700
+++ 25-akpm/include/asm-s390/unistd.h	2004-05-02 22:27:36.568487912 -0700
@@ -360,6 +360,9 @@
 
 /* user-visible error numbers are in the range -1 - -122: see <asm-s390/errno.h> */
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
 #define __syscall_return(type, res)			     \
 do {							     \
 	if ((unsigned long)(res) >= (unsigned long)(-125)) { \
@@ -368,6 +371,7 @@ do {							     \
 	}						     \
 	return (type) (res);				     \
 } while (0)
+#endif
 
 #define _svc_clobber "1", "cc", "memory"
 
diff -puN include/asm-sh/unistd.h~kernel-syscalls-retval-fix include/asm-sh/unistd.h
--- 25/include/asm-sh/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.551490496 -0700
+++ 25-akpm/include/asm-sh/unistd.h	2004-05-02 22:27:36.569487760 -0700
@@ -286,6 +286,9 @@
 
 /* user-visible error numbers are in the range -1 - -124: see <asm-sh/errno.h> */
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
 #define __syscall_return(type, res) \
 do { \
 	if ((unsigned long)(res) >= (unsigned long)(-124)) { \
@@ -297,6 +300,7 @@ do { \
 	} \
 	return (type) (res); \
 } while (0)
+#endif
 
 /* XXX - _foo needs to be __foo, while __NR_bar could be _NR_bar. */
 #define _syscall0(type,name) \
diff -puN include/asm-sparc64/unistd.h~kernel-syscalls-retval-fix include/asm-sparc64/unistd.h
--- 25/include/asm-sparc64/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.552490344 -0700
+++ 25-akpm/include/asm-sparc64/unistd.h	2004-05-02 22:27:36.570487608 -0700
@@ -303,6 +303,16 @@
  *          find a free slot in the 0-282 range.
  */
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
+#define __syscall_return(type, res)		\
+	if (res >= 0)				\
+		return (type)res;		\
+	errno = -res;				\
+	return -1;
+#endif
+
 #define _syscall0(type,name) \
 type name(void) \
 { \
@@ -314,10 +324,7 @@ __asm__ __volatile__ ("t 0x6d\n\t" \
 		      : "=r" (__res)\
 		      : "r" (__g1) \
 		      : "o0", "cc"); \
-if (__res >= 0) \
-    return (type) __res; \
-errno = -__res; \
-return -1; \
+	__syscall_return(type, __res) \
 }
 
 #define _syscall1(type,name,type1,arg1) \
@@ -332,10 +339,7 @@ __asm__ __volatile__ ("t 0x6d\n\t" \
 		      : "=r" (__res), "=&r" (__o0) \
 		      : "1" (__o0), "r" (__g1) \
 		      : "cc"); \
-if (__res >= 0) \
-	return (type) __res; \
-errno = -__res; \
-return -1; \
+	__syscall_return(type, __res) \
 }
 
 #define _syscall2(type,name,type1,arg1,type2,arg2) \
@@ -351,10 +355,7 @@ __asm__ __volatile__ ("t 0x6d\n\t" \
 		      : "=r" (__res), "=&r" (__o0) \
 		      : "1" (__o0), "r" (__o1), "r" (__g1) \
 		      : "cc"); \
-if (__res >= 0) \
-	return (type) __res; \
-errno = -__res; \
-return -1; \
+	__syscall_return(type, __res) \
 }
 
 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
@@ -371,10 +372,7 @@ __asm__ __volatile__ ("t 0x6d\n\t" \
 		      : "=r" (__res), "=&r" (__o0) \
 		      : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__g1) \
 		      : "cc"); \
-if (__res>=0) \
-	return (type) __res; \
-errno = -__res; \
-return -1; \
+	__syscall_return(type, __res) \
 }
 
 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
@@ -392,10 +390,7 @@ __asm__ __volatile__ ("t 0x6d\n\t" \
 		      : "=r" (__res), "=&r" (__o0) \
 		      : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__o3), "r" (__g1) \
 		      : "cc"); \
-if (__res>=0) \
-	return (type) __res; \
-errno = -__res; \
-return -1; \
+	__syscall_return(type, __res) \
 } 
 
 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
@@ -415,10 +410,7 @@ __asm__ __volatile__ ("t 0x6d\n\t" \
 		      : "=r" (__res), "=&r" (__o0) \
 		      : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__o3), "r" (__o4), "r" (__g1) \
 		      : "cc"); \
-if (__res>=0) \
-	return (type) __res; \
-errno = -__res; \
-return -1; \
+	__syscall_return(type, __res) \
 }
 #ifdef __KERNEL_SYSCALLS__
 
diff -puN include/asm-sparc/unistd.h~kernel-syscalls-retval-fix include/asm-sparc/unistd.h
--- 25/include/asm-sparc/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.553490192 -0700
+++ 25-akpm/include/asm-sparc/unistd.h	2004-05-02 22:27:36.571487456 -0700
@@ -302,6 +302,16 @@
  *          find a free slot in the 0-282 range.
  */
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
+#define __syscall_return(type, res)		\
+	if (res < -255 || res >= 0)		\
+		return (type)res;		\
+	errno = -res;				\
+	return -1;
+#endif
+
 #define _syscall0(type,name) \
 type name(void) \
 { \
@@ -315,10 +325,7 @@ __asm__ __volatile__ ("t 0x10\n\t" \
 		      : "=r" (__res)\
 		      : "r" (__g1) \
 		      : "o0", "cc"); \
-if (__res < -255 || __res >= 0) \
-    return (type) __res; \
-errno = -__res; \
-return -1; \
+	__syscall_return(type, __res) \
 }
 
 #define _syscall1(type,name,type1,arg1) \
@@ -335,10 +342,7 @@ __asm__ __volatile__ ("t 0x10\n\t" \
 		      : "=r" (__res), "=&r" (__o0) \
 		      : "1" (__o0), "r" (__g1) \
 		      : "cc"); \
-if (__res < -255 || __res >= 0) \
-	return (type) __res; \
-errno = -__res; \
-return -1; \
+	__syscall_return(type, __res) \
 }
 
 #define _syscall2(type,name,type1,arg1,type2,arg2) \
@@ -356,10 +360,7 @@ __asm__ __volatile__ ("t 0x10\n\t" \
 		      : "=r" (__res), "=&r" (__o0) \
 		      : "1" (__o0), "r" (__o1), "r" (__g1) \
 		      : "cc"); \
-if (__res < -255 || __res >= 0) \
-	return (type) __res; \
-errno = -__res; \
-return -1; \
+	__syscall_return(type, __res) \
 }
 
 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
@@ -378,10 +379,7 @@ __asm__ __volatile__ ("t 0x10\n\t" \
 		      : "=r" (__res), "=&r" (__o0) \
 		      : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__g1) \
 		      : "cc"); \
-if (__res < -255 || __res>=0) \
-	return (type) __res; \
-errno = -__res; \
-return -1; \
+	__syscall_return(type, __res) \
 }
 
 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
@@ -401,10 +399,7 @@ __asm__ __volatile__ ("t 0x10\n\t" \
 		      : "=r" (__res), "=&r" (__o0) \
 		      : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__o3), "r" (__g1) \
 		      : "cc"); \
-if (__res < -255 || __res>=0) \
-	return (type) __res; \
-errno = -__res; \
-return -1; \
+	__syscall_return(type, __res) \
 } 
 
 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
@@ -426,10 +421,7 @@ __asm__ __volatile__ ("t 0x10\n\t" \
 		      : "=r" (__res), "=&r" (__o0) \
 		      : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__o3), "r" (__o4), "r" (__g1) \
 		      : "cc"); \
-if (__res < -255 || __res>=0) \
-	return (type) __res; \
-errno = -__res; \
-return -1; \
+	__syscall_return(type, __res) \
 }
 #ifdef __KERNEL_SYSCALLS__
 
diff -puN include/asm-v850/unistd.h~kernel-syscalls-retval-fix include/asm-v850/unistd.h
--- 25/include/asm-v850/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.554490040 -0700
+++ 25-akpm/include/asm-v850/unistd.h	2004-05-02 22:27:36.571487456 -0700
@@ -245,6 +245,9 @@
 #define __builtin_expect(x, expected_value) (x)
 #endif
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
 #define __syscall_return(type, res)					      \
   do {									      \
 	  /* user-visible error numbers are in the range -1 - -124:	      \
@@ -255,7 +258,7 @@
 	  }								      \
 	  return (type) (res);						      \
   } while (0)
-
+#endif
 
 #define _syscall0(type, name)						      \
 type name (void)							      \
diff -puN include/asm-x86_64/unistd.h~kernel-syscalls-retval-fix include/asm-x86_64/unistd.h
--- 25/include/asm-x86_64/unistd.h~kernel-syscalls-retval-fix	2004-05-02 22:27:36.556489736 -0700
+++ 25-akpm/include/asm-x86_64/unistd.h	2004-05-02 22:27:36.572487304 -0700
@@ -560,6 +560,9 @@ __SYSCALL(__NR_mq_getsetattr, sys_mq_get
 
 #define __syscall_clobber "r11","rcx","memory" 
 
+#ifdef __KERNEL__
+#define __syscall_return(type, res) return ((type)(res))
+#else
 #define __syscall_return(type, res) \
 do { \
 	if ((unsigned long)(res) >= (unsigned long)(-127)) { \
@@ -568,6 +571,7 @@ do { \
 	} \
 	return (type) (res); \
 } while (0)
+#endif
 
 #ifndef __KERNEL_SYSCALLS__
 

_