[PATCH v3] ipc: provide generic compat versions of IPC syscalls
authorChris Metcalf <cmetcalf@tilera.com>
Thu, 15 Mar 2012 17:13:38 +0000 (13:13 -0400)
committerChris Metcalf <cmetcalf@tilera.com>
Thu, 15 Mar 2012 17:13:38 +0000 (13:13 -0400)
When using the "compat" APIs, architectures will generally want to
be able to make direct syscalls to msgsnd(), shmctl(), etc., and
in the kernel we would want them to be handled directly by
compat_sys_xxx() functions, as is true for other compat syscalls.

However, for historical reasons, several of the existing compat IPC
syscalls do not do this.  semctl() expects a pointer to the fourth
argument, instead of the fourth argument itself.  msgsnd(), msgrcv()
and shmat() expect arguments in different order.

This change adds an ARCH_WANT_OLD_COMPAT_IPC config option that can be
set to preserve this behavior for ports that use it (x86, sparc, powerpc,
s390, and mips).  No actual semantics are changed for those architectures,
and there is only a minimal amount of code refactoring in ipc/compat.c.

Newer architectures like tile (and perhaps future architectures such
as arm64 and unicore64) should not select this option, and thus can
avoid having any IPC-specific code at all in their architecture-specific
compat layer.  In the same vein, if this option is not selected, IPC_64
mode is assumed, since that's what the <asm-generic> headers expect.

The workaround code in "tile" for msgsnd() and msgrcv() is removed
with this change; it also fixes the bug that shmat() and semctl() were
not being properly handled.

Reviewed-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Chris Metcalf <cmetcalf@tilera.com>
arch/Kconfig
arch/mips/Kconfig
arch/powerpc/Kconfig
arch/s390/Kconfig
arch/sparc/Kconfig
arch/tile/include/asm/compat.h
arch/tile/kernel/compat.c
arch/x86/Kconfig
include/linux/compat.h
ipc/compat.c

index 4f55c736be11ea3aa3ac6762b0ec3d1371b872ce..b37f8f3ffa547dba4711111831c71423f829e526 100644 (file)
@@ -199,4 +199,7 @@ config HAVE_CMPXCHG_LOCAL
 config HAVE_CMPXCHG_DOUBLE
        bool
 
+config ARCH_WANT_OLD_COMPAT_IPC
+       bool
+
 source "kernel/gcov/Kconfig"
index 5ab6e89603c56bc461de1b4390ada67482749c2e..4bbbb40f352adb3120519bdb64a7a02013ef0db3 100644 (file)
@@ -2456,6 +2456,7 @@ config MIPS32_COMPAT
 config COMPAT
        bool
        depends on MIPS32_COMPAT
+       select ARCH_WANT_OLD_COMPAT_IPC
        default y
 
 config SYSVIPC_COMPAT
index 1919634a9b32c261c6f989f7c87b7403a5c6c57e..48ab0bb38924c7f6a9f484024c882143351610cf 100644 (file)
@@ -152,6 +152,7 @@ config COMPAT
        bool
        default y if PPC64
        select COMPAT_BINFMT_ELF
+       select ARCH_WANT_OLD_COMPAT_IPC
 
 config SYSVIPC_COMPAT
        bool
index 6d99a5fcc09030114dd8d475e6fe54184fc59814..0ff53e350092a6371c15724bc7c2418ffd95e1d2 100644 (file)
@@ -218,6 +218,7 @@ config COMPAT
        prompt "Kernel support for 31 bit emulation"
        depends on 64BIT
        select COMPAT_BINFMT_ELF
+       select ARCH_WANT_OLD_COMPAT_IPC
        help
          Select this option if you want to enable your system kernel to
          handle system-calls from ELF binaries for 31 bit ESA.  This option
index ca5580e4d813711f272c57ddb6b5f43428ed99f9..64e1a8e7cab3e7698bc6aa7342c6fe497c958260 100644 (file)
@@ -576,6 +576,7 @@ config COMPAT
        depends on SPARC64
        default y
        select COMPAT_BINFMT_ELF
+       select ARCH_WANT_OLD_COMPAT_IPC
 
 config SYSVIPC_COMPAT
        bool
index bf95f55b82b04e77c0e93ac8e28391c820ecaa08..4b4b28969a65266f0840f8e25774908054d70ca2 100644 (file)
@@ -242,17 +242,6 @@ long compat_sys_fallocate(int fd, int mode,
 long compat_sys_sched_rr_get_interval(compat_pid_t pid,
                                      struct compat_timespec __user *interval);
 
-/* Versions of compat functions that differ from generic Linux. */
-struct compat_msgbuf;
-long tile_compat_sys_msgsnd(int msqid,
-                           struct compat_msgbuf __user *msgp,
-                           size_t msgsz, int msgflg);
-long tile_compat_sys_msgrcv(int msqid,
-                           struct compat_msgbuf __user *msgp,
-                           size_t msgsz, long msgtyp, int msgflg);
-long tile_compat_sys_ptrace(compat_long_t request, compat_long_t pid,
-                           compat_long_t addr, compat_long_t data);
-
 /* Tilera Linux syscalls that don't have "compat" versions. */
 #define compat_sys_flush_cache sys_flush_cache
 
index bf5e9d70266c72fe9ef45e30cdade23ef0f2e374..d67459b9ac2aa43f4c44eb50125c0143509e873f 100644 (file)
@@ -16,7 +16,6 @@
 #define __SYSCALL_COMPAT
 
 #include <linux/compat.h>
-#include <linux/msg.h>
 #include <linux/syscalls.h>
 #include <linux/kdev_t.h>
 #include <linux/fs.h>
@@ -95,52 +94,10 @@ long compat_sys_sched_rr_get_interval(compat_pid_t pid,
        return ret;
 }
 
-/*
- * The usual compat_sys_msgsnd() and _msgrcv() seem to be assuming
- * some different calling convention than our normal 32-bit tile code.
- */
-
-/* Already defined in ipc/compat.c, but we need it here. */
-struct compat_msgbuf {
-       compat_long_t mtype;
-       char mtext[1];
-};
-
-long tile_compat_sys_msgsnd(int msqid,
-                           struct compat_msgbuf __user *msgp,
-                           size_t msgsz, int msgflg)
-{
-       compat_long_t mtype;
-
-       if (get_user(mtype, &msgp->mtype))
-               return -EFAULT;
-       return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
-}
-
-long tile_compat_sys_msgrcv(int msqid,
-                           struct compat_msgbuf __user *msgp,
-                           size_t msgsz, long msgtyp, int msgflg)
-{
-       long err, mtype;
-
-       err =  do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
-       if (err < 0)
-               goto out;
-
-       if (put_user(mtype, &msgp->mtype))
-               err = -EFAULT;
- out:
-       return err;
-}
-
 /* Provide the compat syscall number to call mapping. */
 #undef __SYSCALL
 #define __SYSCALL(nr, call) [nr] = (call),
 
-/* The generic versions of these don't work for Tile. */
-#define compat_sys_msgrcv tile_compat_sys_msgrcv
-#define compat_sys_msgsnd tile_compat_sys_msgsnd
-
 /* See comments in sys.c */
 #define compat_sys_fadvise64_64 sys32_fadvise64_64
 #define compat_sys_readahead sys32_readahead
index 5bed94e189fab8ef9bf24f038763ee90bc45b798..cde163dc6058f85fd346bea9ffc43d80bc1bb729 100644 (file)
@@ -2178,6 +2178,7 @@ config IA32_AOUT
 config COMPAT
        def_bool y
        depends on IA32_EMULATION
+       select ARCH_WANT_OLD_COMPAT_IPC
 
 config COMPAT_FOR_U64_ALIGNMENT
        def_bool COMPAT
index 7e05fcee75a158815986fe9477fbbf98bde50052..35c2dbf2448abe5085c8242bff22eb10698d4f62 100644 (file)
@@ -224,6 +224,7 @@ struct compat_sysinfo;
 struct compat_sysctl_args;
 struct compat_kexec_segment;
 struct compat_mq_attr;
+struct compat_msgbuf;
 
 extern void compat_exit_robust_list(struct task_struct *curr);
 
@@ -234,13 +235,22 @@ asmlinkage long
 compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
                           compat_size_t __user *len_ptr);
 
+#ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
 long compat_sys_semctl(int first, int second, int third, void __user *uptr);
 long compat_sys_msgsnd(int first, int second, int third, void __user *uptr);
 long compat_sys_msgrcv(int first, int second, int msgtyp, int third,
                int version, void __user *uptr);
-long compat_sys_msgctl(int first, int second, void __user *uptr);
 long compat_sys_shmat(int first, int second, compat_uptr_t third, int version,
                void __user *uptr);
+#else
+long compat_sys_semctl(int semid, int semnum, int cmd, int arg);
+long compat_sys_msgsnd(int msqid, struct compat_msgbuf __user *msgp,
+               size_t msgsz, int msgflg);
+long compat_sys_msgrcv(int msqid, struct compat_msgbuf __user *msgp,
+               size_t msgsz, long msgtyp, int msgflg);
+long compat_sys_shmat(int shmid, compat_uptr_t shmaddr, int shmflg);
+#endif
+long compat_sys_msgctl(int first, int second, void __user *uptr);
 long compat_sys_shmctl(int first, int second, void __user *uptr);
 long compat_sys_semtimedop(int semid, struct sembuf __user *tsems,
                unsigned nsems, const struct compat_timespec __user *timeout);
index 845a28738d3a824e5c7a4cd583c28c9d793467cf..a6df704f521e01f628def526e97b38cca92bb3ed 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/msg.h>
 #include <linux/shm.h>
 #include <linux/syscalls.h>
+#include <linux/ptrace.h>
 
 #include <linux/mutex.h>
 #include <asm/uaccess.h>
@@ -117,6 +118,7 @@ extern int sem_ctls[];
 
 static inline int compat_ipc_parse_version(int *cmd)
 {
+#ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
        int version = *cmd & IPC_64;
 
        /* this is tricky: architectures that have support for the old
@@ -128,6 +130,10 @@ static inline int compat_ipc_parse_version(int *cmd)
        *cmd &= ~IPC_64;
 #endif
        return version;
+#else
+       /* With the asm-generic APIs, we always use the 64-bit versions. */
+       return IPC_64;
+#endif
 }
 
 static inline int __get_compat_ipc64_perm(struct ipc64_perm *p64,
@@ -232,10 +238,9 @@ static inline int put_compat_semid_ds(struct semid64_ds *s,
        return err;
 }
 
-long compat_sys_semctl(int first, int second, int third, void __user *uptr)
+static long do_compat_semctl(int first, int second, int third, u32 pad)
 {
        union semun fourth;
-       u32 pad;
        int err, err2;
        struct semid64_ds s64;
        struct semid64_ds __user *up64;
@@ -243,10 +248,6 @@ long compat_sys_semctl(int first, int second, int third, void __user *uptr)
 
        memset(&s64, 0, sizeof(s64));
 
-       if (!uptr)
-               return -EINVAL;
-       if (get_user(pad, (u32 __user *) uptr))
-               return -EFAULT;
        if ((third & (~IPC_64)) == SETVAL)
                fourth.val = (int) pad;
        else
@@ -305,6 +306,18 @@ long compat_sys_semctl(int first, int second, int third, void __user *uptr)
        return err;
 }
 
+#ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
+long compat_sys_semctl(int first, int second, int third, void __user *uptr)
+{
+       u32 pad;
+
+       if (!uptr)
+               return -EINVAL;
+       if (get_user(pad, (u32 __user *) uptr))
+               return -EFAULT;
+       return do_compat_semctl(first, second, third, pad);
+}
+
 long compat_sys_msgsnd(int first, int second, int third, void __user *uptr)
 {
        struct compat_msgbuf __user *up = uptr;
@@ -353,6 +366,37 @@ long compat_sys_msgrcv(int first, int second, int msgtyp, int third,
 out:
        return err;
 }
+#else
+long compat_sys_semctl(int semid, int semnum, int cmd, int arg)
+{
+       return do_compat_semctl(semid, semnum, cmd, arg);
+}
+
+long compat_sys_msgsnd(int msqid, struct compat_msgbuf __user *msgp,
+                      size_t msgsz, int msgflg)
+{
+       compat_long_t mtype;
+
+       if (get_user(mtype, &msgp->mtype))
+               return -EFAULT;
+       return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
+}
+
+long compat_sys_msgrcv(int msqid, struct compat_msgbuf __user *msgp,
+                      size_t msgsz, long msgtyp, int msgflg)
+{
+       long err, mtype;
+
+       err =  do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
+       if (err < 0)
+               goto out;
+
+       if (put_user(mtype, &msgp->mtype))
+               err = -EFAULT;
+ out:
+       return err;
+}
+#endif
 
 static inline int get_compat_msqid64(struct msqid64_ds *m64,
                                     struct compat_msqid64_ds __user *up64)
@@ -470,6 +514,7 @@ long compat_sys_msgctl(int first, int second, void __user *uptr)
        return err;
 }
 
+#ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
 long compat_sys_shmat(int first, int second, compat_uptr_t third, int version,
                        void __user *uptr)
 {
@@ -485,6 +530,19 @@ long compat_sys_shmat(int first, int second, compat_uptr_t third, int version,
        uaddr = compat_ptr(third);
        return put_user(raddr, uaddr);
 }
+#else
+long compat_sys_shmat(int shmid, compat_uptr_t shmaddr, int shmflg)
+{
+       unsigned long ret;
+       long err;
+
+       err = do_shmat(shmid, compat_ptr(shmaddr), shmflg, &ret);
+       if (err)
+               return err;
+       force_successful_syscall_return();
+       return (long)ret;
+}
+#endif
 
 static inline int get_compat_shmid64_ds(struct shmid64_ds *s64,
                                        struct compat_shmid64_ds __user *up64)