remove ksys_mount() and ksys_dup()
Subsystems (3)
The following changes since commit ae4b064e2a616b545acf02b8f50cc513b32c7522:
Merge tag 'afs-fixes-20191211' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs (2019-12-11 18:10:40 -0800)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/brodo/linux.git remove-ksys-mount-dup
for you to fetch changes up to 8243186f0cc7c57cf9d6a110cd7315c44e3e0be8:
fs: remove ksys_dup() (2019-12-12 19:00:36 +0100)
This small series replaces all in-kernel calls to the userspace-focused ksys_mount() and ksys_dup() with calls to kernel-centric functions:
For each replacement of ksys_mount() with do_mount(), one needs to verify that the first and third parameter (char *dev_name, char *type) are strings allocated in kernelspace and that the fifth parameter (void *data) is either NULL or refers to a full page (only occurence in init/do_mounts.c::do_mount_root()). The second and fourth parameters (char *dir_name, unsigned long flags) are passed by ksys_mount() to do_mount() unchanged, and therefore do not require particular care.
Moreover, instead of pretending to be userspace, the opening of /dev/console as stdin/stdout/stderr can be implemented using in-kernel functions as well. Thereby, ksys_dup() can be removed for good.
---
Changes since the split-series patches[1,2]:
- merge both series (on suggestion by Linus)
- remove pointless cast (on suggestion by Linus)
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
Thanks,
Dominik
----------------------------------------------------------------
Dominik Brodowski (5):
devtmpfs: use do_mount() instead of ksys_mount()
initrd: use do_mount() instead of ksys_mount()
init: use do_mount() instead of ksys_mount()
init: unify opening /dev/console as stdin/stdout/stderr
fs: remove ksys_dup()
drivers/base/devtmpfs.c | 6 +++--- fs/file.c | 7 +------ fs/namespace.c | 10 ++-------- include/linux/device.h | 4 ++-- include/linux/initrd.h | 2 ++ include/linux/syscalls.h | 3 --- init/do_mounts.c | 30 +++++++++++++++++++++++------- init/do_mounts_initrd.c | 11 ++++------- init/main.c | 31 ++++++++++++++++++++++++++----- 9 files changed, 63 insertions(+), 41 deletions(-)
Signature · 1 line
2.24.1
PATCH 1/5 devtmpfs: use do_mount() instead of ksys_mount()
In devtmpfs, do_mount() can be called directly instead of complex wrapping by ksys_mount():
- the first and third arguments are const strings in the kernel, and do not need to be copied over from userspace;
- the fifth argument is NULL, and therefore no page needs to be copied over from userspace;
- the second and fourth argument are passed through anyway.
--- drivers/base/devtmpfs.c | 6 +++--- include/linux/device.h | 4 ++-- init/do_mounts.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-)
drivers/base/devtmpfs.c +3 -3
@@ -359,7 +359,7 @@ static int handle_remove(const char *nodename, struct device *dev)* If configured, or requested by the commandline, devtmpfs will be * auto-mounted after the kernel mounted the root filesystem. */ -int devtmpfs_mount (const char *mntdir) +int devtmpfs_mount (void ) {int err;
@@ -369,7 +369,7 @@ int devtmpfs_mount(const char *mntdir)if (!thread)return 0 ; - err =ksys_mount ("devtmpfs" , mntdir,"devtmpfs" ,MS_SILENT ,NULL ); + err =do_mount ("devtmpfs" ,"dev" ,"devtmpfs" ,MS_SILENT ,NULL );if (err)printk (KERN_INFO "devtmpfs: error mounting %i\n" , err);else
@@ -394,7 +394,7 @@ static int devtmpfsd(void *p) *err =ksys_unshare (CLONE_NEWNS );if (*err) goto out; - *err =ksys_mount ("devtmpfs" ,"/" ,"devtmpfs" ,MS_SILENT ,NULL ); + *err =do_mount ("devtmpfs" ,"/" ,"devtmpfs" ,MS_SILENT ,NULL );if (*err) goto out;ksys_chdir ("/.." );/* will traverse into overmounted root */
include/linux/device.h +2 -2
@@ -1666,11 +1666,11 @@ extern bool kill_device(struct device *dev);#ifdef CONFIG_DEVTMPFS extern int devtmpfs_create_node (struct device *dev);extern int devtmpfs_delete_node (struct device *dev); -extern int devtmpfs_mount (const char *mntdir); +extern int devtmpfs_mount (void );#else static inline int devtmpfs_create_node (struct device *dev) {return 0 ; }static inline int devtmpfs_delete_node (struct device *dev) {return 0 ; } -static inline int devtmpfs_mount (const char *mountpoint) {return 0 ; } +static inline int devtmpfs_mount (void ) {return 0 ; }#endif /* drivers/base/power/shutdown.c */
init/do_mounts.c +1 -1
@@ -670,7 +670,7 @@ void __init prepare_namespace(void)mount_root (); out: -devtmpfs_mount ("dev" ); +devtmpfs_mount ();ksys_mount ("." ,"/" ,NULL ,MS_MOVE ,NULL );ksys_chroot ("." ); }
Signature · 1 line
2.24.1
PATCH 2/5 initrd: use do_mount() instead of ksys_mount()
In reply to Dominik BrodowskiAll three calls to ksys_mount() in initrd-related kernel code can be switched over to do_mount():
- the first and third arguments are const strings in the kernel, and do not need to be copied over from userspace;
- the fifth argument is NULL, and therefore no page needs to be, copied over from userspace;
- the second and fourth argument are passed through anyway.
--- init/do_mounts_initrd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
init/do_mounts_initrd.c +3 -3
@@ -54,7 +54,7 @@ static int init_linuxrc(struct subprocess_info *info, struct cred *new)ksys_dup (0 );/* move initrd over / and chdir/chroot in initrd root */ ksys_chdir ("/root" ); -ksys_mount ("." ,"/" ,NULL ,MS_MOVE ,NULL ); +do_mount ("." ,"/" ,NULL ,MS_MOVE ,NULL );ksys_chroot ("." );ksys_setsid ();return 0 ;
@@ -89,7 +89,7 @@ static void __init handle_initrd(void) current->flags &= ~PF_FREEZER_SKIP ;/* move initrd to rootfs' /old */ -ksys_mount (".." ,"." ,NULL ,MS_MOVE ,NULL ); +do_mount (".." ,"." ,NULL ,MS_MOVE ,NULL );/* switch root and cwd back to / of rootfs */ ksys_chroot (".." );
@@ -103,7 +103,7 @@ static void __init handle_initrd(void)mount_root ();printk (KERN_NOTICE "Trying to move old root to /initrd ... " ); - error =ksys_mount ("/old" ,"/root/initrd" ,NULL ,MS_MOVE ,NULL ); + error =do_mount ("/old" ,"/root/initrd" ,NULL ,MS_MOVE ,NULL );if (!error)printk ("okay\n" );else {
Signature · 1 line
2.24.1
PATCH 3/5 init: use do_mount() instead of ksys_mount()
In reply to Dominik BrodowskiIn prepare_namespace(), do_mount() can be used instead of ksys_mount() as the first and third argument are const strings in the kernel, the second and fourth argument are passed through anyway, and the fifth argument is NULL.
In do_mount_root(), ksys_mount() is called with the first and third argument being already kernelspace strings, which do not need to be copied over from userspace to kernelspace (again). The second and fourth arguments are passed through to do_mount() anyway. The fifth argument, while already residing in kernelspace, needs to be put into a page of its own. Then, do_mount() can be used instead of ksys_mount().
Once this is done, there are no in-kernel users to ksys_mount() left, which can therefore be removed.
--- fs/namespace.c | 10 ++-------- include/linux/syscalls.h | 2 -- init/do_mounts.c | 28 ++++++++++++++++++++++------ 3 files changed, 24 insertions(+), 16 deletions(-)
fs/namespace.c +2 -8
@@ -3325,8 +3325,8 @@ struct dentry *mount_subtree(struct vfsmount *m, const char *name) }EXPORT_SYMBOL (mount_subtree); -int ksys_mount (const char __user *dev_name,const char __user *dir_name, -const char __user *type,unsigned long flags,void __user *data) +SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, + char __user *, type, unsigned long, flags, void __user *, data) {int ret;char *kernel_type;
@@ -3359,12 +3359,6 @@ int ksys_mount(const char __user *dev_name, const char __user *dir_name,return ret; } -SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, - char __user *, type, unsigned long, flags, void __user *, data) -{ -return ksys_mount (dev_name, dir_name, type, flags, data); -} -/* * Create a kernel mount representation for a new, prepared superblock * (specified by fs_fd) and attach to an open_tree-like file descriptor.
include/linux/syscalls.h +0 -2
@@ -1231,8 +1231,6 @@ asmlinkage long sys_ni_syscall(void);* the ksys_xyzyyz() functions prototyped below. */ -int ksys_mount (const char __user *dev_name,const char __user *dir_name, -const char __user *type,unsigned long flags,void __user *data);int ksys_umount (char __user *name,int flags);int ksys_dup (unsigned int fildes);int ksys_chroot (const char __user *filename);
init/do_mounts.c +22 -6
@@ -387,12 +387,25 @@ static void __init get_fs_names(char *page) *s ='\0' ; } -static int __init do_mount_root (char *name,char *fs,int flags,void *data) +static int __init do_mount_root (const char *name,const char *fs, +const int flags,const void *data) {struct super_block *s; -int err =ksys_mount (name,"/root" , fs, flags, data); -if (err) -return err; +char *data_page; +struct page *p; +int ret; + +/* do_mount() requires a full page as fifth argument */ + p =alloc_page (GFP_KERNEL ); +if (!p) +return -ENOMEM ; + + data_page =page_address (p); +strncpy (data_page, data,PAGE_SIZE -1 ); + + ret =do_mount (name,"/root" , fs, flags, data_page); +if (ret) + goto out;ksys_chdir ("/root" ); s = current->fs->pwd.dentry->d_sb;
@@ -402,7 +415,10 @@ static int __init do_mount_root(char *name, char *fs, int flags, void *data) s->s_type->name,sb_rdonly (s) ?" readonly" :"" , MAJOR(ROOT_DEV), MINOR(ROOT_DEV)); -return 0 ; + +out: +put_page (p); +return ret; }void __init mount_block_root (char *name,int flags)
@@ -671,7 +687,7 @@ void __init prepare_namespace(void)mount_root (); out:devtmpfs_mount (); -ksys_mount ("." ,"/" ,NULL ,MS_MOVE ,NULL ); +do_mount ("." ,"/" ,NULL ,MS_MOVE ,NULL );ksys_chroot ("." ); }
Signature · 1 line
2.24.1
Re: [PATCH 3/5] init: use do_mount() instead of ksys_mount()
Quoted 16 lines · Dominik Brodowski
init/do_mounts.c +2
-4@@ -387,12 +387,25 @@ static void __init get_fs_names(char *page) *s ='\0' ; } -static int __init do_mount_root (char *name,char *fs,int flags,void *data) +static int __init do_mount_root (const char *name,const char *fs, +const int flags,const void *data) {struct super_block *s; -int err =ksys_mount (name,"/root" , fs, flags, data); -if (err) -return err;
init/do_mounts.c static void __init get_fs_names(char *page) · line 403
Quoted 10 lines
init/do_mounts.c +10
-0+char *data_page; +struct page *p; +int ret; + +/* do_mount() requires a full page as fifth argument */ + p =alloc_page (GFP_KERNEL ); +if (!p) +return -ENOMEM ; + + data_page =page_address (p);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
That doesn't work in my guest as it gives a funny address:
[ 3.155314] mount_block_root: entry [ 3.155868] mount_block_root: fs_name: [ext3] [ 3.156512] do_mount_root: will copy data page: 0x00000000adf0ddb8
leading to the splat below.
Reverting the patch fixes the boot.
Thx.
[ 3.575074] BUG: kernel NULL pointer dereference, address: 0000000000000000 [ 3.576858] #PF: supervisor read access in kernel mode [ 3.578274] #PF: error_code(0x0000) - not-present page [ 3.579003] PGD 0 P4D 0 [ 3.579003] Oops: 0000 [#1] PREEMPT SMP [ 3.579003] CPU: 8 PID: 1 Comm: swapper/0 Not tainted 5.5.0-rc1+ #17 [ 3.579003] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.1-1 04/01/2014 [ 3.579003] RIP: 0010:strncpy+0xf/0x30 [ 3.579003] Code: 0f b6 0c 16 88 0c 10 48 ff c2 84 c9 75 f2 f3 c3 66 66 2e 0f 1f 84 00 00 00 00 00 48 85 d2 48 89 f8 74 1b 4c 8d 04 17 48 89 fa <0f> b6 0e 80 f9 01 88 0a 48 83 de ff 48 ff c2 4c 39 c2 75 ec f3 c3 [ 3.579003] RSP: 0018:ffffc90000013eb8 EFLAGS: 00010206 [ 3.579003] RAX: ffff88807b780000 RBX: 0000000000008001 RCX: 0000000000000000 [ 3.579003] RDX: ffff88807b780000 RSI: 0000000000000000 RDI: ffff88807b780000 [ 3.579003] RBP: ffff88807b781000 R08: ffff88807b780fff R09: 00000000000770f4 [ 3.579003] R10: 0000000000000000 R11: 0000000000000000 R12: ffff88807b781000 [ 3.579003] R13: 0000000000000000 R14: 0000000000000000 R15: ffffea0001ede000 [ 3.579003] FS: 0000000000000000(0000) GS:ffff88807dc00000(0000) knlGS:0000000000000000 [ 3.579003] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 3.579003] CR2: 0000000000000000 CR3: 0000000002009000 CR4: 00000000003406e0 [ 3.579003] Call Trace: [ 3.579003] mount_block_root+0x14f/0x312 [ 3.579003] prepare_namespace+0x136/0x165 [ 3.579003] ? rest_init+0xb9/0xb9 [ 3.579003] kernel_init+0xa/0xf7 [ 3.579003] ret_from_fork+0x22/0x40 [ 3.579003] Modules linked in: [ 3.579003] CR2: 0000000000000000 [ 3.579003] ---[ end trace 2884b7e501f1daa6 ]--- [ 3.579003] RIP: 0010:strncpy+0xf/0x30 [ 3.579003] Code: 0f b6 0c 16 88 0c 10 48 ff c2 84 c9 75 f2 f3 c3 66 66 2e 0f 1f 84 00 00 00 00 00 48 85 d2 48 89 f8 74 1b 4c 8d 04 17 48 89 fa <0f> b6 0e 80 f9 01 88 0a 48 83 de ff 48 ff c2 4c 39 c2 75 ec f3 c3 [ 3.579003] RSP: 0018:ffffc90000013eb8 EFLAGS: 00010206 [ 3.579003] RAX: ffff88807b780000 RBX: 0000000000008001 RCX: 0000000000000000 [ 3.579003] RDX: ffff88807b780000 RSI: 0000000000000000 RDI: ffff88807b780000 [ 3.579003] RBP: ffff88807b781000 R08: ffff88807b780fff R09: 00000000000770f4 [ 3.579003] R10: 0000000000000000 R11: 0000000000000000 R12: ffff88807b781000 [ 3.579003] R13: 0000000000000000 R14: 0000000000000000 R15: ffffea0001ede000 [ 3.579003] FS: 0000000000000000(0000) GS:ffff88807dc00000(0000) knlGS:0000000000000000 [ 3.579003] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 3.579003] CR2: 0000000000000000 CR3: 0000000002009000 CR4: 00000000003406e0 [ 3.611795] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000009 [ 3.612923] Kernel Offset: disabled [ 3.613505] ---[ end Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000009 ]---
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH 3/5] init: use do_mount() instead of ksys_mount()
Quoted 29 lines · Borislav Petkov
Quoted 16 lines · Dominik Brodowski
init/do_mounts.c +2
-4@@ -387,12 +387,25 @@ static void __init get_fs_names(char *page) *s ='\0' ; } -static int __init do_mount_root (char *name,char *fs,int flags,void *data) +static int __init do_mount_root (const char *name,const char *fs, +const int flags,const void *data) {struct super_block *s; -int err =ksys_mount (name,"/root" , fs, flags, data); -if (err) -return err;init/do_mounts.c static void __init get_fs_names(char *page) · line 403
Quoted 10 lines
init/do_mounts.c +10
-0+char *data_page; +struct page *p; +int ret; + +/* do_mount() requires a full page as fifth argument */ + p =alloc_page (GFP_KERNEL ); +if (!p) +return -ENOMEM ; + + data_page =page_address (p);^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 7 lines
That doesn't work in my guest as it gives a funny address:
[ 3.155314] mount_block_root: entry [ 3.155868] mount_block_root: fs_name: [ext3] [ 3.156512] do_mount_root: will copy data page: 0x00000000adf0ddb8leading to the splat below.
Does
https://lore.kernel.org/lkml/CAHk-=wh8VLe3AEKhz=1bzSO=1fv4EM71EhufxuC=Gp=+bLhXoA@mail.gmail.com/
solve the issue?
Thanks,
Dominik
Borislav Petkov
Tested-byRe: [PATCH 3/5] init: use do_mount() instead of ksys_mount()
Quoted 5 lines · Dominik Brodowski
Does
https://lore.kernel.org/lkml/CAHk-=wh8VLe3AEKhz=1bzSO=1fv4EM71EhufxuC=Gp=+bLhXoA@mail.gmail.com/
solve the issue?
Yap, it does.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH 4/5 init: unify opening /dev/console as stdin/stdout/stderr
In reply to Dominik BrodowskiMerge the two instances where /dev/console is opened as stdin/stdout/stderr.
--- include/linux/initrd.h | 2 ++ init/do_mounts_initrd.c | 5 +---- init/main.c | 17 ++++++++++++----- 3 files changed, 15 insertions(+), 9 deletions(-)
include/linux/initrd.h +2 -0
@@ -28,3 +28,5 @@ extern unsigned int real_root_dev;extern char __initramfs_start[];extern unsigned long __initramfs_size; + +void console_on_rootfs (void );
init/do_mounts_initrd.c +1 -4
@@ -48,10 +48,7 @@ early_param("initrd", early_initrd);static int init_linuxrc (struct subprocess_info *info,struct cred *new) {ksys_unshare (CLONE_FS |CLONE_FILES ); -/* stdin/stdout/stderr for /linuxrc */ -ksys_open ("/dev/console" ,O_RDWR ,0 ); -ksys_dup (0 ); -ksys_dup (0 ); +console_on_rootfs ();/* move initrd over / and chdir/chroot in initrd root */ ksys_chdir ("/root" );do_mount ("." ,"/" ,NULL ,MS_MOVE ,NULL );
init/main.c +12 -5
@@ -1155,6 +1155,17 @@ static int __ref kernel_init(void *unused) "See Linux Documentation/admin-guide/init.rst for guidance."); } +void console_on_rootfs (void ) +{ +/* Open the /dev/console as stdin, this should never fail */ +if (ksys_open ((const char __user *)"/dev/console" ,O_RDWR ,0 ) <0 ) +pr_err ("Warning: unable to open an initial console.\n" ); + +/* create stdout/stderr */ + (void )ksys_dup (0 ); + (void )ksys_dup (0 ); +} + static noinline void __init kernel_init_freeable(void) {/*
@@ -1190,12 +1201,8 @@ static noinline void __init kernel_init_freeable(void)do_basic_setup (); -/* Open the /dev/console on the rootfs, this should never fail */ -if (ksys_open ((const char __user *)"/dev/console" ,O_RDWR ,0 ) <0 ) -pr_err ("Warning: unable to open an initial console.\n" ); +console_on_rootfs (); - (void )ksys_dup (0 ); - (void )ksys_dup (0 );/* * check if there is an early userspace init. If yes, let it do all * the work
Signature · 1 line
2.24.1
PATCH 5/5 fs: remove ksys_dup()
In reply to Dominik Brodowskiksys_dup() is used only at one place in the kernel, namely to duplicate fd 0 of /dev/console to stdout and stderr. The same functionality can be achieved by using functions already available within the kernel namespace.
--- fs/file.c | 7 +------ include/linux/syscalls.h | 1 - init/main.c | 26 ++++++++++++++++++++------ 3 files changed, 21 insertions(+), 13 deletions(-)
fs/file.c +1 -6
@@ -960,7 +960,7 @@ SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)return ksys_dup3 (oldfd, newfd,0 ); } -int ksys_dup (unsigned int fildes) +SYSCALL_DEFINE1(dup, unsigned int, fildes) {int ret = -EBADF ;struct file *file =fget_raw (fildes);
@@ -975,11 +975,6 @@ int ksys_dup(unsigned int fildes)return ret; } -SYSCALL_DEFINE1(dup, unsigned int, fildes) -{ -return ksys_dup (fildes); -} -int f_dupfd (unsigned int from,struct file *file,unsigned flags) {int err;
include/linux/syscalls.h +0 -1
@@ -1232,7 +1232,6 @@ asmlinkage long sys_ni_syscall(void);*/ int ksys_umount (char __user *name,int flags); -int ksys_dup (unsigned int fildes);int ksys_chroot (const char __user *filename);ssize_t ksys_write (unsigned int fd,const char __user *buf,size_t count);int ksys_chdir (const char __user *filename);
init/main.c +20 -6
@@ -93,6 +93,7 @@#include <linux/rodata_test.h> #include <linux/jump_label.h> #include <linux/mem_encrypt.h> +#include <linux/file.h> #include <asm/io.h> #include <asm/bugs.h>
@@ -1157,13 +1158,26 @@ static int __ref kernel_init(void *unused)void console_on_rootfs (void ) { -/* Open the /dev/console as stdin, this should never fail */ -if (ksys_open ((const char __user *)"/dev/console" ,O_RDWR ,0 ) <0 ) -pr_err ("Warning: unable to open an initial console.\n" ); +struct file *file; +unsigned int i; + +/* Open /dev/console in kernelspace, this should never fail */ + file =filp_open ("/dev/console" ,O_RDWR ,0 ); +if (!file) + goto err_out; + +/* create stdin/stdout/stderr, this should never fail */ +for (i =0 ; i <3 ; i++) { +if (f_dupfd (i, file,0 ) != i) + goto err_out; + } + +return ; -/* create stdout/stderr */ - (void )ksys_dup (0 ); - (void )ksys_dup (0 ); +err_out: +/* no panic -- this might not be fatal */ +pr_err ("Warning: unable to open an initial console.\n" ); +return ; } static noinline void __init kernel_init_freeable(void)
Signature · 1 line
2.24.1
Quoted 4 lines · Dominik Brodowski
the fifth parameter (void *data)
is either NULL or refers to a full page (only occurence in init/do_mounts.c::do_mount_root()).
We probably should aim for the fifth parameter being a "buf, len" pair at some point.
Then the system call interface still needs to copy the whole page and pass in PAGE_SIZE as the length, but it would be a better model than the magical fixed "it's always one page". And the kernel init sequence wouldn't need that silly temporary page any more.
Linus
The pull request you sent on Thu, 12 Dec 2019 19:14:17 +0100:
The pull request you sent on Thu, 12 Dec 2019 19:14:17 +0100:
Quoted 1 line
git://git.kernel.org/pub/scm/linux/kernel/git/brodo/linux.gitremove-ksys-mount-dup
has been merged into torvalds/linux.git:
https://git.kernel.org/torvalds/c/2e6d304515ba9936d85265ad93dddc4c13c17d06
Thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.wiki.kernel.org/userdoc/prtracker
A message that is not in the archive (sent off-list or not yet received): CAJmaN=ksaH5AgRUdVPGWKZzjEinU+goaCqedH1PW6OmKYc_TuA@mail.gmail.com
Greg Kroah-Hartman
maintainerQuoted 5 lines · Jesse Barnes
Still debugging, but this causes a panic in
console_on_rootfs()when we try to dup the fds for stderr and stdout. Probably because in my config I have VT and framebuffer console disabled?
Reverting8243186f0cc7c57cf9d6a110cd7315c44e3e0be8
andb49a733d684e0096340b93e9dfd471f0e3ddc06dworked around it for now...
Should already be fixed in Linus's tree.
thanks,
greg k-h
Quoted 9 lines · Greg Kroah-Hartman
Quoted 5 lines · Jesse Barnes
Still debugging, but this causes a panic in
console_on_rootfs()when we try to dup the fds for stderr and stdout. Probably because in my config I have VT and framebuffer console disabled?
Reverting8243186f0cc7c57cf9d6a110cd7315c44e3e0be8
andb49a733d684e0096340b93e9dfd471f0e3ddc06dworked around it for now...Should already be fixed in Linus's tree.
Arg sorry for the html mail. I see the reverts now.
Thanks,
Jesse
Quoted 2 lines · Jesse Barnes
Still debugging, but this causes a panic in
console_on_rootfs()when we try to dup the fds for stderr and stdout.
Duh.
That series was incredibly buggy, and there's another bug in there.
I think this should fix it:
diff --git a/init/main.c b/init/main.c index ec3a1463ac69..1ecfd43ed464 100644 --- a/init/main.c +++ b/init/main.c @@ -1163,7 +1163,7 @@ void console_on_rootfs(void)
/* Open /dev/console in kernelspace, this should never fail */
file = filp_open("/dev/console", O_RDWR, 0);
- if (!file)
+ if (IS_ERR(file))
goto err_out;
/* create stdin/stdout/stderr, this should never fail */and yes,that particular problem only triggers when you have some odd root filesystem without a /dev/console. Or a kernel config that doesn't have those devices enabled at all.
I delayed pulling it for a couple of days, but the branch was not in linux-next, so my delay didn't make any difference, and all these things only became obvious after I pulled. And while it was all horribly buggy, it was only buggy for the "these cases don't happen in a normal distro" case, so the regular use didn't show them.
My bad. I shouldn't have pulled this, but it all looked very obvious and trivial.
Linus
Quoted 29 lines · Linus Torvalds
Quoted 2 lines · Jesse Barnes
Still debugging, but this causes a panic in
console_on_rootfs()when we try to dup the fds for stderr and stdout.Duh.
That series was incredibly buggy, and there's another bug in there.
I think this should fix it:
diff --git a/init/main.c b/init/main.c index ec3a1463ac69..1ecfd43ed464 100644 --- a/init/main.c +++ b/init/main.c @@ -1163,7 +1163,7 @@ void console_on_rootfs(void)/* Open /dev/console in kernelspace, this should never fail */ file = filp_open("/dev/console", O_RDWR, 0); - if (!file) + if (IS_ERR(file)) goto err_out; /* create stdin/stdout/stderr, this should never fail */and yes,that particular problem only triggers when you have some odd root filesystem without a
/dev/console. Or a kernel config that doesn't have those devices enabled at all.
Quoted 8 lines
I delayed pulling it for a couple of days, but the branch was not in linux-next, so my delay didn't make any difference, and all these things only became obvious after I pulled. And while it was all horribly buggy, it was only buggy for the "these cases don't happen in a normal distro" case, so the regular use didn't show them.
My bad. I shouldn't have pulled this, but it all looked very obvious and trivial.
Oh I should have caught that too, I was looking right at it...
But anyway it looks like a nice cleanup with a few more fixes. Hopefully we can get there soon...
Thanks,
Jesse
Al Viro
maintainerQuoted 10 lines · Jesse Barnes
Quoted 10 lines
and yes,that particular problem only triggers when you have some odd root filesystem without a
/dev/console. Or a kernel config that doesn't have those devices enabled at all.I delayed pulling it for a couple of days, but the branch was not in linux-next, so my delay didn't make any difference, and all these things only became obvious after I pulled. And while it was all horribly buggy, it was only buggy for the "these cases don't happen in a normal distro" case, so the regular use didn't show them.
Quoted 7 lines
Quoted 2 lines
My bad. I shouldn't have pulled this, but it all looked very obvious and trivial.
Oh I should have caught that too, I was looking right at it...
But anyway it looks like a nice cleanup with a few more fixes. Hopefully we can get there soon...
FWIW, this is precisely what I'd been talking about[*] - instead of a plain "we are reusing the damn syscall, with fixed interface and debugged by userland all the time" we'd got an open-coded analogue that will be a headache (and a source of bitrot) for years.
It's not a normal part of the kernel, and I bloody well remember what kind of headache it had been before it got massaged to use of plain syscalls. Constant need to remember that a change in VFS guts might break something in the code that is hell to debug - getting test coverage for it is not fun at all. As we are seeing right now...
Seriously, these parts of init/* ought to be treated as userland code that runs in kernel mode mostly because it's too much PITA to arrange building a static ELF binary and linking it into the image.
[*] "IMO it's not a good idea. Exposing the guts of fs/namespace.c to what's essentially a userland code that happens to run in kernel thread is asking for trouble - we'd been there and it had been hell to untangle."
My fault, I guess - should've been more specific than that ;-/
Al Viro
maintainerQuoted 36 lines · Al Viro
Quoted 10 lines · Jesse Barnes
Quoted 10 lines
and yes,that particular problem only triggers when you have some odd root filesystem without a
/dev/console. Or a kernel config that doesn't have those devices enabled at all.I delayed pulling it for a couple of days, but the branch was not in linux-next, so my delay didn't make any difference, and all these things only became obvious after I pulled. And while it was all horribly buggy, it was only buggy for the "these cases don't happen in a normal distro" case, so the regular use didn't show them.
Quoted 7 lines
Quoted 2 lines
My bad. I shouldn't have pulled this, but it all looked very obvious and trivial.
Oh I should have caught that too, I was looking right at it...
But anyway it looks like a nice cleanup with a few more fixes. Hopefully we can get there soon...
FWIW, this is precisely what I'd been talking about[*] - instead of a plain "we are reusing the damn syscall, with fixed interface and debugged by userland all the time" we'd got an open-coded analogue that will be a headache (and a source of bitrot) for years.
It's not a normal part of the kernel, and I bloody well remember what kind of headache it had been before it got massaged to use of plain syscalls. Constant need to remember that a change in VFS guts might break something in the code that is hell to debug - getting test coverage for it is not fun at all. As we are seeing right now...
Seriously, these parts of
init/*ought to be treated as userland code that runs in kernel mode mostly because it's too much PITA to arrange building a static ELF binary and linking it into the image.
Quoted 5 lines
[*] "IMO it's not a good idea. Exposing the guts of
fs/namespace.cto what's essentially a userland code that happens to run in kernel thread is asking for trouble - we'd been there and it had been hell to untangle."My fault, I guess - should've been more specific than that ;-/
PS: please, don't take that kind of stuff any further; right now all that thing does is marshalling the arguments. At that level it's just going to be a headache while debugging that code. Take it further (e.g. play with calling do_move_mount() et.al. instead of using MS_MOVE) and the headache will be ongoing, not just one-time. "Just use ksys_...() in init/*.c" prevented that kind of stuff; now that this policy no longer holds, we'd better watch out for trouble in that area.
To quote the original patchset, "instead of pretending to be userspace, ... can be implemented using using in-kernel functions" and exact same rationale would lead to a lot of trouble. That's what I'm really worried about; let's not go there.
Quoted 3 lines · Al Viro
Seriously, these parts of
init/*ought to be treated as userland code that runs in kernel mode mostly because it's too much PITA to arrange building a static ELF binary and linking it into the image.
Well, we have had the infrastructure for fork_usermode_blob() in the kernel since May 2018, though it is not really used so far (the bpfilter blob is just reporting its existence, and not doing anything substantial). Probably, significant parts of init/* could be migrated to such a blob. Would that be an alternative generally preferred, or is its dependence on CC_CAN_LINK a showstopper?
FWIW, non-initramfs boot code is considered to be "legacy" since 2.6.16, see filesystems/ramfs-rootfs-initramfs.txt:
| Today (2.6.16), initramfs is always compiled in, but not always used. The | kernel falls back to legacy boot code that is reached only if initramfs does | not contain an /init program. The fallback is legacy code, there to ensure a | smooth transition and allowing early boot functionality to gradually move to | "early userspace" (I.E. initramfs). | | ... | | This kind of complexity (which inevitably includes policy) is rightly handled | in userspace. Both klibc and busybox/uClibc are working on simple initramfs | packages to drop into a kernel build. | | The klibc package has now been accepted into Andrew Morton's 2.6.17-mm tree. | The kernel's current early boot code (partition detection, etc) will probably | be migrated into a default initramfs, automatically created and used by the | kernel build.
That plan seems to have been obsoleted long ago, right?
Thanks,
Dominik
Al Viro
maintainerQuoted 11 lines · Dominik Brodowski
Quoted 3 lines · Al Viro
Seriously, these parts of
init/*ought to be treated as userland code that runs in kernel mode mostly because it's too much PITA to arrange building a static ELF binary and linking it into the image.Well, we have had the infrastructure for
fork_usermode_blob()in the kernel since May 2018, though it is not really used so far (the bpfilter blob is just reporting its existence, and not doing anything substantial). Probably, significant parts ofinit/*could be migrated to such a blob. Would that be an alternative generally preferred, or is its dependence onCC_CAN_LINKa showstopper?
Well... everything from default_rootfs/populate_rootfs call (incidentally, stuff starting to leak into rootfs_initcall level shouldn't be mixed with those two, but that's a separate story) and on to the end of kernel_init_freeable() could be forked; the other bit (actual execve attempts in the end of kernel_init()) must be in PID 1.
The problem is not just CC_CAN_LINK, it's the damn size of binaries...
Quoted 20 lines
FWIW, non-initramfs boot code is considered to be "legacy" since 2.6.16, see
filesystems/ramfs-rootfs-initramfs.txt:| Today (2.6.16), initramfs is always compiled in, but not always used. The | kernel falls back to legacy boot code that is reached only if initramfs does | not contain an /init program. The fallback is legacy code, there to ensure a | smooth transition and allowing early boot functionality to gradually move to | "early userspace" (I.E. initramfs). | | ... | | This kind of complexity (which inevitably includes policy) is rightly handled | in userspace. Both klibc and busybox/uClibc are working on simple initramfs | packages to drop into a kernel build. | | The klibc package has now been accepted into Andrew Morton's 2.6.17-mm tree. | The kernel's current early boot code (partition detection, etc) will probably | be migrated into a default initramfs, automatically created and used by the | kernel build.
Quoted 1 line
That plan seems to have been obsoleted long ago, right?
klibc is not in mainline and I hadn't heard of attempts to get it into the kernel git tree for quite a few years. Whether this "just call sys_...() instead of doing normal syscalls" is a stopgap measure until that happens or something more permanent, the effect is the same - not poking in the kernel internals from code with lousy test coverage...
Re: [PATCH 4/5] init: unify opening /dev/console as stdin/stdout/stderr
In reply to Dominik Brodowskiit caused Androidx86 /system/bin/sh: No controlling tty: open /dev/tty: No such device or address /system/bin/sh: warning: won't have full job control
Androidx86 alt+f1 root on tmpfs, alt+f2/f3 root on rootfs.
Re: [PATCH 4/5] init: unify opening /dev/console as stdin/stdout/stderr
Quoted 4 lines · On Tue, Dec 17, 2019 at 01:17:51PM +0800, youling257 wrote:
it caused Androidx86 /system/bin/sh: No controlling tty: open
/dev/tty: No such device or address /system/bin/sh: warning: won't have full job controlAndroidx86 alt+f1 root on tmpfs, alt+f2/f3 root on rootfs.
Are you sure it is caused by the patch you reference? It's really only moving code around, and does not depend on tmpfs/rootfs. The exact same three calls are made before and after the change, see
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b49a733d684e0096340b93e9dfd471f0e3ddc06d
The preceding patch (3/5) needs a bugfix which already is upstream, though.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7de7de7ca0ae0fc70515ee3154af33af75edae2c
Thanks,
Dominik
Re: [PATCH 4/5] init: unify opening /dev/console as stdin/stdout/stderr
I had been Revert "fs: remove ksys_dup()" Revert "init: unify opening /dev/console as stdin/stdout/stderr", test boot, n the system/bin/sh warning.
2019-12-17 14:42 GMT+08:00, Dominik Brodowski <[email protected]>:
Quoted 18 lines · On Tue, Dec 17, 2019 at 01:17:51PM +0800, youling257 wrote:
Quoted 5 lines
it caused Androidx86 /system/bin/sh: No controlling tty: open
/dev/tty: No such device or address
/system/bin/sh: warning: won't have full job controlAndroidx86 alt+f1 root on tmpfs, alt+f2/f3 root on rootfs.
Are you sure it is caused by the patch you reference? It's really only moving code around, and does not depend on tmpfs/rootfs. The exact same three calls are made before and after the change, see
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b49a733d684e0096340b93e9dfd471f0e3ddc06dThe preceding patch (3/5) needs a bugfix which already is upstream, though.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7de7de7ca0ae0fc70515ee3154af33af75edae2cThanks,
Dominik
Re: [PATCH 4/5] init: unify opening /dev/console as stdin/stdout/stderr
This should be fixed by 2d3145f8d280 ("early init: fix error handling when opening /dev/console")
I'm not sure what you did to trigger that bug, but it was a bug.
Linus
Quoted 4 lines · On Tue, Dec 17, 2019 at 1:34 AM youling 257 <[email protected]> wrote:
I had been Revert "fs: remove
ksys_dup()" Revert "init: unify opening/dev/consoleas stdin/stdout/stderr", test boot, n the system/bin/sh warning.
Re: [PATCH 4/5] init: unify opening /dev/console as stdin/stdout/stderr
only Revert "fs: remove ksys_dup()", not see the warning "/system/bin/sh: No controlling tty: open /dev/tty: No such device or address."
yes, "fs: remove ksys_dup()" caused my system/bin/sh problem.
but test "early init: fix error handling when opening /dev/console", still can see "/system/bin/sh: No controlling tty" warning, it not solve my problem.
2019-12-18 5:14 GMT+08:00, Linus Torvalds <[email protected]>:
Quoted 14 lines
This should be fixed by
2d3145f8d280("early init: fix error handling when opening/dev/console")I'm not sure what you did to trigger that bug, but it was a bug.
Linus
Quoted 4 lines · On Tue, Dec 17, 2019 at 1:34 AM youling 257 <[email protected]> wrote:
I had been Revert "fs: remove
ksys_dup()" Revert "init: unify opening/dev/consoleas stdin/stdout/stderr", test boot, n the system/bin/sh warning.
Re: [PATCH 4/5] init: unify opening /dev/console as stdin/stdout/stderr
Quoted 9 lines · On Wed, Dec 18, 2019 at 12:10:08PM +0800, youling 257 wrote:
only Revert "fs: remove
ksys_dup()", not see the warning "/system/bin/sh: No controlling tty: open/dev/tty: No such device or address."yes, "fs: remove
ksys_dup()" caused my system/bin/sh problem.but test "early init: fix error handling when opening
/dev/console", still can see "/system/bin/sh: No controlling tty" warning, it not solve my problem.
FWIW, I see a similar (seemingly harmless) warning both _before_ and after my patches, so I am not sure that this is a real regression.
Thanks,
Dominik
Re: [PATCH 4/5] init: unify opening /dev/console as stdin/stdout/stderr
In reply to Linus Torvalds2019-12-18 5:14 GMT+08:00, Linus Torvalds <[email protected]>:
Quoted 2 lines
This should be fixed by
2d3145f8d280("early init: fix error handling when opening/dev/console")
this fix no help for me.
Quoted 1 line
I'm not sure what you did to trigger that bug, but it was a bug.
alt+f1, type bash command,
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
Re: [PATCH 4/5] init: unify opening /dev/console as stdin/stdout/stderr
Quoted 11 lines · On Thu, Dec 19, 2019 at 05:50:19AM +0800, youling 257 wrote:
2019-12-18 5:14 GMT+08:00, Linus Torvalds <[email protected]>:
Quoted 2 lines
This should be fixed by
2d3145f8d280("early init: fix error handling when opening/dev/console")this fix no help for me.
Quoted 1 line
I'm not sure what you did to trigger that bug, but it was a bug.
alt+f1, type bash command,
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
Could you test this patch, please? And if it does not work: What is the content of /proc/1/fd/ and /proc/1/fdinfo/* for the working and non-working case? That are the only changes visible to userspace...
Thanks,
Dominik
init/main.c +1 -1
@@ -1162,7 +1162,7 @@ void console_on_rootfs(void)unsigned int i;/* Open /dev/console in kernelspace, this should never fail */ - file =filp_open ("/dev/console" ,O_RDWR ,0 ); + file =filp_open ("/dev/console" ,force_o_largefile () ?O_LARGEFILE |O_RDWR :O_RDWR ,0 );if (IS_ERR (file)) goto err_out;
Re: [PATCH 4/5] init: unify opening /dev/console as stdin/stdout/stderr
Test this patch not work, still has system/bin/sh warning. ls -al /proc/1/fd
total 0
dr-x------ 2 root root 0 2019-12-19 17:19 [1;34m. [0m dr-xr-xr-x 9 root root 0 2019-12-19 17:19 [1;34m.. [0m lrwx------ 1 root root 64 2019-12-19 17:19 [1;36m0 [0m ->
[1;31m/android/sys/fs/selinux/null [0m
lrwx------ 1 root root 64 2019-12-19 17:19 [1;36m1 [0m ->
[1;31m/android/sys/fs/selinux/null [0m
lrwx------ 1 root root 64 2019-12-19 17:19 [1;36m10 [0m ->
[1;31msocket:[12337] [0m
l-wx------ 1 root root 64 2019-12-19 17:19 [1;36m11 [0m ->
[1;31m/android/dev/pmsg0 [0m
lrwx------ 1 root root 64 2019-12-19 17:19 [1;36m2 [0m ->
[1;31m/android/sys/fs/selinux/null [0m
l-wx------ 1 root root 64 2019-12-19 17:19 [1;36m3 [0m ->
[1;31m/android/dev/__kmsg__ (deleted) [0m
lrwx------ 1 root root 64 2019-12-19 17:19 [1;36m4 [0m ->
[1;31manon_inode:[eventpoll] [0m
lrwx------ 1 root root 64 2019-12-19 17:19 [1;36m5 [0m ->
[1;31msocket:[857] [0m
lrwx------ 1 root root 64 2019-12-19 17:19 [1;36m6 [0m ->
[1;31msocket:[858] [0m
lrwx------ 1 root root 64 2019-12-19 17:19 [1;36m7 [0m ->
[1;31msocket:[859] [0m
lrwx------ 1 root root 64 2019-12-19 17:19 [1;36m9 [0m ->
[1;31msocket:[10698] [0m
Revert "fs: remove ksys_dup()", no the system/bin/sh warning. ls -al /proc/1/fd
total 0
dr-x------ 2 root root 0 2019-12-19 17:28 [1;34m. [0m dr-xr-xr-x 9 root root 0 2019-12-19 17:28 [1;34m.. [0m lrwx------ 1 root root 64 2019-12-19 17:28 [1;36m0 [0m ->
[1;31m/android/sys/fs/selinux/null [0m
lrwx------ 1 root root 64 2019-12-19 17:28 [1;36m1 [0m ->
[1;31m/android/sys/fs/selinux/null [0m
lrwx------ 1 root root 64 2019-12-19 17:28 [1;36m10 [0m ->
[1;31msocket:[12506] [0m
l-wx------ 1 root root 64 2019-12-19 17:28 [1;36m11 [0m ->
[1;31m/android/dev/pmsg0 [0m
lrwx------ 1 root root 64 2019-12-19 17:28 [1;36m2 [0m ->
[1;31m/android/sys/fs/selinux/null [0m
l-wx------ 1 root root 64 2019-12-19 17:28 [1;36m3 [0m ->
[1;31m/android/dev/__kmsg__ (deleted) [0m
lrwx------ 1 root root 64 2019-12-19 17:28 [1;36m4 [0m ->
[1;31manon_inode:[eventpoll] [0m
lrwx------ 1 root root 64 2019-12-19 17:28 [1;36m5 [0m ->
[1;31msocket:[1957] [0m
lrwx------ 1 root root 64 2019-12-19 17:28 [1;36m6 [0m ->
[1;31msocket:[1958] [0m
lrwx------ 1 root root 64 2019-12-19 17:28 [1;36m7 [0m ->
[1;31msocket:[1959] [0m
lrwx------ 1 root root 64 2019-12-19 17:28 [1;36m9 [0m ->
[1;31msocket:[2040] [0m
2019-12-19 15:08 GMT+08:00, Dominik Brodowski <[email protected]>:
Quoted 37 lines · On Thu, Dec 19, 2019 at 05:50:19AM +0800, youling 257 wrote:
Quoted 5 lines
2019-12-18 5:14 GMT+08:00, Linus Torvalds
<[email protected]>:Quoted 2 lines
This should be fixed by
2d3145f8d280("early init: fix error handling when opening/dev/console")Quoted 8 lines
this fix no help for me.
Quoted 1 line
I'm not sure what you did to trigger that bug, but it was a bug.
alt+f1, type bash command,
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shellCould you test this patch, please? And if it does not work: What is the content of
/proc/1/fd/and/proc/1/fdinfo/*for the working and non-working case? That are the only changes visible to userspace...Thanks,
Dominikinit/main.c +1
-1@@ -1162,7 +1162,7 @@ void console_on_rootfs(void)unsigned int i;/* Open /dev/console in kernelspace, this should never fail */ - file =filp_open ("/dev/console" ,O_RDWR ,0 ); + file =filp_open ("/dev/console" ,force_o_largefile () ?O_LARGEFILE |O_RDWR :O_RDWR ,0 );if (IS_ERR (file)) goto err_out;