Skip to contentSkip to conversation

asmlinkage_protect replaces prevent_tail_call

PATCH New Roland McGrath 8 participants 10 messages Apr 10, 2008 – Apr 11, 2008 7 messages not in the archive

Subsystems (4)
lore
Share & tools
lore
#7 lore raw

PATCH 1/2 asmlinkage_protect replaces prevent_tail_call

In reply to a message not in the archive

The prevent_tail_call() macro works around the problem of the compiler clobbering argument words on the stack, which for asmlinkage functions is the caller's (user's) struct pt_regs. The tail/sibling-call optimization is not the only way that the compiler can decide to use stack argument words as scratch space, which we have to prevent. Other optimizations can do it too.

Until we have new compiler support to make "asmlinkage" binding on the compiler's own use of the stack argument frame, we have work around all the manifestations of this issue that crop up.

More cases seem to be prevented by also keeping the incoming argument variables live at the end of the function. This makes their original stack slots attractive places to leave those variables, so the compiler tends not clobber them for something else. It's still no guarantee, but it handles some observed cases that prevent_tail_call() did not.

---
 arch/x86/kernel/tls.c     |    4 ++--
 fs/open.c                 |    8 ++++----
 include/asm-x86/linkage.h |   24 +++++++++++++++++++++++-
 include/linux/linkage.h   |    4 ++--
 kernel/exit.c             |    4 ++--
 kernel/uid16.c            |   22 +++++++++++-----------
 6 files changed, 44 insertions(+), 22 deletions(-)
arch/x86/kernel/tls.c +2 -2
index 022bcaa..ab6bf37 100644
@@ -92,7 +92,7 @@ int do_set_thread_area(struct task_struct *p, int idx,
 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
 {
 	int ret = do_set_thread_area(current, -1, u_info, 1);
-	prevent_tail_call(ret);
+	asmlinkage_protect(1, ret, u_info);
 	return ret;
 }
 
@@ -142,7 +142,7 @@ int do_get_thread_area(struct task_struct *p, int idx,
 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
 {
 	int ret = do_get_thread_area(current, -1, u_info);
-	prevent_tail_call(ret);
+	asmlinkage_protect(1, ret, u_info);
 	return ret;
 }
 
fs/open.c +4 -4
index a4b1202..3fa4e4f 100644
@@ -335,7 +335,7 @@ asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
 {
 	long ret = do_sys_ftruncate(fd, length, 1);
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(2, ret, fd, length);
 	return ret;
 }
 
@@ -350,7 +350,7 @@ asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
 {
 	long ret = do_sys_ftruncate(fd, length, 0);
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(2, ret, fd, length);
 	return ret;
 }
 #endif
@@ -1067,7 +1067,7 @@ asmlinkage long sys_open(const char __user *filename, int flags, int mode)
 
 	ret = do_sys_open(AT_FDCWD, filename, flags, mode);
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(3, ret, filename, flags, mode);
 	return ret;
 }
 
@@ -1081,7 +1081,7 @@ asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
 
 	ret = do_sys_open(dfd, filename, flags, mode);
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(4, ret, dfd, filename, flags, mode);
 	return ret;
 }
 
include/asm-x86/linkage.h +23 -1
index 31739c7..d605eeb 100644
@@ -8,12 +8,34 @@
 
 #ifdef CONFIG_X86_32
 #define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0)))
-#define prevent_tail_call(ret) __asm__ ("" : "=r" (ret) : "0" (ret))
 /*
  * For 32-bit UML - mark functions implemented in assembly that use
  * regparm input parameters:
  */
 #define asmregparm __attribute__((regparm(3)))
+
+#define asmlinkage_protect(n, ret, args...) \
+	__asmlinkage_protect##n(ret, ##args)
+#define __asmlinkage_protect_n(ret, args...) \
+	__asm__ __volatile__ ("" : "=r" (ret) : "0" (ret), ##args)
+#define __asmlinkage_protect0(ret) \
+	__asmlinkage_protect_n(ret)
+#define __asmlinkage_protect1(ret, arg1) \
+	__asmlinkage_protect_n(ret, "g" (arg1))
+#define __asmlinkage_protect2(ret, arg1, arg2) \
+	__asmlinkage_protect_n(ret, "g" (arg1), "g" (arg2))
+#define __asmlinkage_protect3(ret, arg1, arg2, arg3) \
+	__asmlinkage_protect_n(ret, "g" (arg1), "g" (arg2), "g" (arg3))
+#define __asmlinkage_protect4(ret, arg1, arg2, arg3, arg4) \
+	__asmlinkage_protect_n(ret, "g" (arg1), "g" (arg2), "g" (arg3), \
+			      "g" (arg4))
+#define __asmlinkage_protect5(ret, arg1, arg2, arg3, arg4, arg5) \
+	__asmlinkage_protect_n(ret, "g" (arg1), "g" (arg2), "g" (arg3), \
+			      "g" (arg4), "g" (arg5))
+#define __asmlinkage_protect6(ret, arg1, arg2, arg3, arg4, arg5, arg6) \
+	__asmlinkage_protect_n(ret, "g" (arg1), "g" (arg2), "g" (arg3), \
+			      "g" (arg4), "g" (arg5), "g" (arg6))
+
 #endif
 
 #ifdef CONFIG_X86_ALIGNMENT_16
include/linux/linkage.h +2 -2
index 0592936..fe2a39c 100644
@@ -17,8 +17,8 @@
 # define asmregparm
 #endif
 
-#ifndef prevent_tail_call
-# define prevent_tail_call(ret) do { } while (0)
+#ifndef asmlinkage_protect
+# define asmlinkage_protect(n, ret, args...)	do { } while (0)
 #endif
 
 #ifndef __ALIGN
kernel/exit.c +2 -2
index 53872bf..073005b 100644
@@ -1608,7 +1608,7 @@ asmlinkage long sys_waitid(int which, pid_t upid,
 	put_pid(pid);
 
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(5, ret, which, upid, infop, options, ru);
 	return ret;
 }
 
@@ -1640,7 +1640,7 @@ asmlinkage long sys_wait4(pid_t upid, int __user *stat_addr,
 	put_pid(pid);
 
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(4, ret, upid, stat_addr, options, ru);
 	return ret;
 }
 
kernel/uid16.c +11 -11
index dd308ba..3e41c16 100644
@@ -21,7 +21,7 @@ asmlinkage long sys_chown16(const char __user * filename, old_uid_t user, old_gi
 {
 	long ret = sys_chown(filename, low2highuid(user), low2highgid(group));
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(3, ret, filename, user, group);
 	return ret;
 }
 
@@ -29,7 +29,7 @@ asmlinkage long sys_lchown16(const char __user * filename, old_uid_t user, old_g
 {
 	long ret = sys_lchown(filename, low2highuid(user), low2highgid(group));
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(3, ret, filename, user, group);
 	return ret;
 }
 
@@ -37,7 +37,7 @@ asmlinkage long sys_fchown16(unsigned int fd, old_uid_t user, old_gid_t group)
 {
 	long ret = sys_fchown(fd, low2highuid(user), low2highgid(group));
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(3, ret, fd, user, group);
 	return ret;
 }
 
@@ -45,7 +45,7 @@ asmlinkage long sys_setregid16(old_gid_t rgid, old_gid_t egid)
 {
 	long ret = sys_setregid(low2highgid(rgid), low2highgid(egid));
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(2, ret, rgid, egid);
 	return ret;
 }
 
@@ -53,7 +53,7 @@ asmlinkage long sys_setgid16(old_gid_t gid)
 {
 	long ret = sys_setgid(low2highgid(gid));
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(1, ret, gid);
 	return ret;
 }
 
@@ -61,7 +61,7 @@ asmlinkage long sys_setreuid16(old_uid_t ruid, old_uid_t euid)
 {
 	long ret = sys_setreuid(low2highuid(ruid), low2highuid(euid));
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(2, ret, ruid, euid);
 	return ret;
 }
 
@@ -69,7 +69,7 @@ asmlinkage long sys_setuid16(old_uid_t uid)
 {
 	long ret = sys_setuid(low2highuid(uid));
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(1, ret, uid);
 	return ret;
 }
 
@@ -78,7 +78,7 @@ asmlinkage long sys_setresuid16(old_uid_t ruid, old_uid_t euid, old_uid_t suid)
 	long ret = sys_setresuid(low2highuid(ruid), low2highuid(euid),
 				 low2highuid(suid));
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(3, ret, ruid, euid, suid);
 	return ret;
 }
 
@@ -98,7 +98,7 @@ asmlinkage long sys_setresgid16(old_gid_t rgid, old_gid_t egid, old_gid_t sgid)
 	long ret = sys_setresgid(low2highgid(rgid), low2highgid(egid),
 				 low2highgid(sgid));
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(3, ret, rgid, egid, sgid);
 	return ret;
 }
 
@@ -117,7 +117,7 @@ asmlinkage long sys_setfsuid16(old_uid_t uid)
 {
 	long ret = sys_setfsuid(low2highuid(uid));
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(1, ret, uid);
 	return ret;
 }
 
@@ -125,7 +125,7 @@ asmlinkage long sys_setfsgid16(old_gid_t gid)
 {
 	long ret = sys_setfsgid(low2highgid(gid));
 	/* avoid REGPARM breakage on x86: */
-	prevent_tail_call(ret);
+	asmlinkage_protect(1, ret, gid);
 	return ret;
 }
 
#16 lore raw

PATCH 2/2 asmlinkage_protect sys_io_getevents

In reply to a message not in the archive

Use asmlinkage_protect in sys_io_getevents, because GCC for i386 with CONFIG_FRAME_POINTER=n can decide to clobber an argument word on the stack, i.e. the user struct pt_regs. Here the problem is not a tail call, but just the compiler's use of the stack when it inlines and optimizes the body of the called function. This seems to avoid it.

---
 fs/aio.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
fs/aio.c +1 -0
index 6af9219..60a4cd4 100644
@@ -1790,6 +1790,7 @@ asmlinkage long sys_io_getevents(aio_context_t ctx_id,
 		put_ioctx(ioctx);
 	}
 
+	asmlinkage_protect(5, ret, ctx_id, min_nr, nr, events, timeout);
 	return ret;
 }
 
#8 lore raw

PATCH 1/1 Fix compile breakage caused by asmlinkage_protect

In reply to Roland McGrath

From: Heiko Carstens <[email protected]>

git commit 54a015104136974262afa4b8ddd943ea70dec8a2 "asmlinkage_protect replaces prevent_tail_call" causes this build failure on s390:

AS arch/s390/kernel/entry64.o
In file included from arch/s390/kernel/entry64.S:14:
include/linux/linkage.h:34: error: syntax error in macro parameter list make[1]: *** [arch/s390/kernel/entry64.o] Error 1
make: *** [arch/s390/kernel] Error 2

So just surround the new define with an #ifndef __ASSEMBLY__ to prevent any side effects on asm code.

Cc Roland McGrath <[email protected]>
Cc Martin Schwidefsky <[email protected]>
---
 include/linux/linkage.h |    2 ++
 1 file changed, 2 insertions(+)
include/linux/linkage.h +2 -0
Index: linux-2.6/include/linux/linkage.h
===================================================================
@@ -30,9 +30,11 @@
  * protection to work (ie no more work that the compiler might
  * end up needing stack temporaries for).
  */
+#ifndef __ASSEMBLY__
 #ifndef asmlinkage_protect
 # define asmlinkage_protect(n, ret, args...)	do { } while (0)
 #endif
+#endif
 
 #ifndef __ALIGN
 #define __ALIGN		.align 4,0x90
#9 lore raw

Re: [PATCH] Fix compile breakage caused by asmlinkage_protect

Quoted 10 lines · Heiko Carstens

git commit 54a015104136974262afa4b8ddd943ea70dec8a2 "asmlinkage_protect replaces prevent_tail_call" causes this build failure on s390:

AS arch/s390/kernel/entry64.o
In file included from arch/s390/kernel/entry64.S:14:
include/linux/linkage.h:34: error: syntax error in macro parameter list make[1]: *** [arch/s390/kernel/entry64.o] Error 1
make: *** [arch/s390/kernel] Error 2

Ok, that's just _odd_.

Quoted 2 lines

So just surround the new define with an #ifndef __ASSEMBLY__ to prevent any side effects on asm code.

There are no side effects on asm code. It just adds a #define that obviously won't be used.

Is the s390 assembler using some strange C pre-processor that is different from the main C preprocessor and doesn't understand this pattern?

I really think you should fix *that*, because otherwise you'll hit these kinds of bugs occasionally. There aren't that many asm files, it's not worth it optimizing them to use some faster-but-stupider preprocessor.

Linus

#10 lore raw

Re: [PATCH] Fix compile breakage caused by asmlinkage_protect

Quoted 6 lines · Linus Torvalds
Quoted 5 lines · Heiko Carstens

git commit 54a015104136974262afa4b8ddd943ea70dec8a2 "asmlinkage_protect replaces prevent_tail_call" causes this build failure on s390:

Quoted 7 lines
Quoted 5 lines

AS arch/s390/kernel/entry64.o
In file included from arch/s390/kernel/entry64.S:14:
include/linux/linkage.h:34: error: syntax error in macro parameter list make[1]: *** [arch/s390/kernel/entry64.o] Error 1
make: *** [arch/s390/kernel] Error 2

Ok, that's just _odd_.

Not if (some) kernel assembly files are preprocessed with -traditional-cpp or -traditional, which supports neither GNU style arg... nor ISO C99 ... + __VA_ARGS__ vararg macros.

Jakub

maintainer
#11 lore raw

Re: [PATCH] Fix compile breakage caused by asmlinkage_protect

In reply to Linus Torvalds
Quoted 8 lines · Linus Torvalds

Ok, that's just _odd_.

Quoted 2 lines

So just surround the new define with an #ifndef __ASSEMBLY__ to prevent any side effects on asm code.

There are no side effects on asm code. It just adds a #define that obviously won't be used.

Quoted 6 lines

Is the s390 assembler using some strange C pre-processor that is different from the main C preprocessor and doesn't understand this pattern?

I really think you should fix *that*, because otherwise you'll hit these kinds of bugs occasionally. There aren't that many asm files, it's not worth it optimizing them to use some faster-but-stupider preprocessor.

FWIW, at least m68k and m32r cross-builds hit the same. I think I've a very good guess about the reasons:
arch/m32r/kernel/Makefile:EXTRA_AFLAGS := -traditional arch/m68k/fpsp040/Makefile:EXTRA_AFLAGS := -traditional arch/m68k/ifpsp060/Makefile:EXTRA_AFLAGS := -traditional arch/m68k/kernel/Makefile:EXTRA_AFLAGS := -traditional arch/m68k/lib/Makefile:EXTRA_AFLAGS := -traditional arch/m68k/math-emu/Makefile:EXTRA_AFLAGS := -traditional
arch/parisc/kernel/Makefile:AFLAGS_entry.o := -traditional
arch/parisc/kernel/Makefile:AFLAGS_pacache.o := -traditional arch/s390/kernel/Makefile:EXTRA_AFLAGS := -traditional arch/s390/lib/Makefile:EXTRA_AFLAGS := -traditional arch/s390/math-emu/Makefile:EXTRA_AFLAGS := -traditional

and that gets us -traditional-cpp passed to cc1, with obvious resulting unhappiness from vararg macro.

#12 lore raw

Re: [PATCH] Fix compile breakage caused by asmlinkage_protect

Quoted 3 lines · Al Viro

and that gets us -traditional-cpp passed to cc1, with obvious resulting unhappiness from vararg macro.

Yeah, I figured it out eventually.

I do think the architectures should try to avoid it, if only because x86 doesn't use -traditional (so they'll hit things like this unnecessarily otherwise), but I'll apply Heiko's minimal patch in the meantime.

Linus

#13 lore raw

Re: [PATCH] Fix compile breakage caused by asmlinkage_protect

Quoted 4 lines · Linus Torvalds
Quoted 1 line · Al Viro
Quoted 9 lines
Quoted 2 lines

and that gets us -traditional-cpp passed to cc1, with obvious resulting unhappiness from vararg macro.

Yeah, I figured it out eventually.

I do think the architectures should try to avoid it, if only because x86 doesn't use -traditional (so they'll hit things like this unnecessarily otherwise), but I'll apply Heiko's minimal patch in the meantime.

Cool with me; I'll try to puzzle out why removing -traditional breaks on those two specific files. Ugh, big cleanups likely.

cheers, Kyle

#14 lore raw

Re: [PATCH] Fix compile breakage caused by asmlinkage_protect

Quoted 7 lines · Kyle McMartin
Quoted 6 lines · Linus Torvalds
Quoted 2 lines · Al Viro

and that gets us -traditional-cpp passed to cc1, with obvious resulting unhappiness from vararg macro.

Yeah, I figured it out eventually.

Quoted 7 lines
Quoted 4 lines

I do think the architectures should try to avoid it, if only because x86 doesn't use -traditional (so they'll hit things like this unnecessarily otherwise), but I'll apply Heiko's minimal patch in the meantime.

Cool with me; I'll try to puzzle out why removing -traditional breaks on those two specific files. Ugh, big cleanups likely.

So there is at least one architecture that really requires -traditional, it is not an option to just remove them. For s390 the kernel compiles fine without -traditional on the AFLAGS so we can as well remove them. I'll queue the patch below. In the meantime Heikos patch will have to do.

Signature · 11 lines
blue skies,
  Martin.

"Reality continues to ruin my life." - Calvin.

---
[PATCH] s390: remove -traditional from AFLAGS

From: Martin Schwidefsky <[email protected]>
Signed-off-by: Martin Schwidefsky <[email protected]>
---
arch/s390/kernel/Makefile +1 -1
index 4d3e383..cfb6ccc 100644
@@ -2,7 +2,7 @@
 # Makefile for the linux kernel.
 #
 
-EXTRA_AFLAGS	:= -traditional
+EXTRA_AFLAGS	:= 
 
 #
 # Passing null pointers is ok for smp code, since we access the lowcore here.
arch/s390/lib/Makefile +1 -1
index 5208443..dd6f9f5 100644
@@ -2,7 +2,7 @@
 # Makefile for s390-specific library files..
 #
 
-EXTRA_AFLAGS := -traditional
+EXTRA_AFLAGS := 
 
 lib-y += delay.o string.o uaccess_std.o uaccess_pt.o
 obj-$(CONFIG_32BIT) += div64.o qrnnd.o
arch/s390/math-emu/Makefile +1 -1
index 73b3e72..42828d3 100644
@@ -5,4 +5,4 @@
 obj-$(CONFIG_MATHEMU) := math.o
 
 EXTRA_CFLAGS := -I$(src) -Iinclude/math-emu -w
-EXTRA_AFLAGS := -traditional
+EXTRA_AFLAGS := 
#15 lore raw

Re: [PATCH] Fix compile breakage caused by asmlinkage_protect

arch/s390/kernel/Makefile line 5

Quoted 10 lines · Martin Schwidefsky

...

arch/s390/kernel/Makefile +1 -1
@@ -2,7 +2,7 @@
 # Makefile for the linux kernel.
 #
 
-EXTRA_AFLAGS	:= -traditional
+EXTRA_AFLAGS	:= 
...

You can simply ditch the line.

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed