Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

fs: add ksys_dup{,3}() helper; remove in-kernel calls to sys_dup{,3}()

Using ksys_dup() and ksys_dup3() as helper functions allows us to
avoid the in-kernel calls to the sys_dup() and sys_dup3() syscalls.
The ksys_ prefix denotes that these functions are meant as a drop-in
replacement for the syscalls. In particular, they use the same
calling convention as sys_dup{,3}().

In the near future, the fs-external callers of ksys_dup{,3}() should be
converted to call do_dup2() directly. Then, ksys_dup{,3}() can be moved
within sys_dup{,3}() again.

This patch is part of a series which removes in-kernel calls to syscalls.
On this basis, the syscall entry path can be streamlined. For details, see
http://lkml.kernel.org/r/20180325162527.GA17492@light.dominikbrodowski.net

Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>

+18 -7
+13 -3
fs/file.c
··· 870 870 return err; 871 871 } 872 872 873 - SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags) 873 + static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags) 874 874 { 875 875 int err = -EBADF; 876 876 struct file *file; ··· 904 904 return err; 905 905 } 906 906 907 + SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags) 908 + { 909 + return ksys_dup3(oldfd, newfd, flags); 910 + } 911 + 907 912 SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd) 908 913 { 909 914 if (unlikely(newfd == oldfd)) { /* corner case */ ··· 921 916 rcu_read_unlock(); 922 917 return retval; 923 918 } 924 - return sys_dup3(oldfd, newfd, 0); 919 + return ksys_dup3(oldfd, newfd, 0); 925 920 } 926 921 927 - SYSCALL_DEFINE1(dup, unsigned int, fildes) 922 + int ksys_dup(unsigned int fildes) 928 923 { 929 924 int ret = -EBADF; 930 925 struct file *file = fget_raw(fildes); ··· 937 932 fput(file); 938 933 } 939 934 return ret; 935 + } 936 + 937 + SYSCALL_DEFINE1(dup, unsigned int, fildes) 938 + { 939 + return ksys_dup(fildes); 940 940 } 941 941 942 942 int f_dupfd(unsigned int from, struct file *file, unsigned flags)
+1
include/linux/syscalls.h
··· 949 949 int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type, 950 950 unsigned long flags, void __user *data); 951 951 int ksys_umount(char __user *name, int flags); 952 + int ksys_dup(unsigned int fildes); 952 953 953 954 #endif
+2 -2
init/do_mounts_initrd.c
··· 39 39 sys_unshare(CLONE_FS | CLONE_FILES); 40 40 /* stdin/stdout/stderr for /linuxrc */ 41 41 sys_open("/dev/console", O_RDWR, 0); 42 - sys_dup(0); 43 - sys_dup(0); 42 + ksys_dup(0); 43 + ksys_dup(0); 44 44 /* move initrd over / and chdir/chroot in initrd root */ 45 45 sys_chdir("/root"); 46 46 ksys_mount(".", "/", NULL, MS_MOVE, NULL);
+2 -2
init/main.c
··· 1077 1077 if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0) 1078 1078 pr_err("Warning: unable to open an initial console.\n"); 1079 1079 1080 - (void) sys_dup(0); 1081 - (void) sys_dup(0); 1080 + (void) ksys_dup(0); 1081 + (void) ksys_dup(0); 1082 1082 /* 1083 1083 * check if there is an early userspace init. If yes, let it do all 1084 1084 * the work