init: open code setting up stdin/stdout/stderr
authorChristoph Hellwig <hch@lst.de>
Sun, 7 Jun 2020 15:44:28 +0000 (17:44 +0200)
committerChristoph Hellwig <hch@lst.de>
Fri, 31 Jul 2020 06:16:00 +0000 (08:16 +0200)
Don't rely on the implicit set_fs(KERNEL_DS) for ksys_open to work, but
instead open a struct file for /dev/console and then install it as FD
0/1/2 manually.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
init/main.c

index 0ead83e..db0621d 100644 (file)
@@ -1457,15 +1457,19 @@ static int __ref kernel_init(void *unused)
              "See Linux Documentation/admin-guide/init.rst for guidance.");
 }
 
+/* Open /dev/console, for stdin/stdout/stderr, this should never fail */
 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 = filp_open("/dev/console", O_RDWR, 0);
 
-       /* create stdout/stderr */
-       (void) ksys_dup(0);
-       (void) ksys_dup(0);
+       if (IS_ERR(file)) {
+               pr_err("Warning: unable to open an initial console.\n");
+               return;
+       }
+       get_file_rcu_many(file, 2);
+       fd_install(get_unused_fd_flags(0), file);
+       fd_install(get_unused_fd_flags(0), file);
+       fd_install(get_unused_fd_flags(0), file);
 }
 
 static noinline void __init kernel_init_freeable(void)