init: fix init_dup

From: Christoph Hellwig
Date: Mon Aug 03 2020 - 09:56:18 EST


Don't allocate an unused fd for each call. Also drop the extra
reference from filp_open after the init_dup calls while we're at it.

Fixes: 36e96b411649 ("init: add an init_dup helper")
Reported-by Stephen Rothwell <sfr@xxxxxxxxxxxxxxxx>
Signed-off-by: Christoph Hellwig <hch@xxxxxx>
---
fs/init.c | 2 +-
init/main.c | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/init.c b/fs/init.c
index 730e05acda2392..e9c320a48cf157 100644
--- a/fs/init.c
+++ b/fs/init.c
@@ -260,6 +260,6 @@ int __init init_dup(struct file *file)
fd = get_unused_fd_flags(0);
if (fd < 0)
return fd;
- fd_install(get_unused_fd_flags(0), get_file(file));
+ fd_install(fd, get_file(file));
return 0;
}
diff --git a/init/main.c b/init/main.c
index 089e21504b1fc1..9dae9c4f806bb9 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1470,6 +1470,7 @@ void __init console_on_rootfs(void)
init_dup(file);
init_dup(file);
init_dup(file);
+ fput(file);
}

static noinline void __init kernel_init_freeable(void)
--
2.27.0