Commit 18112d46 authored by Yu Ma's avatar Yu Ma Committed by Jinjie Ruan
Browse files

fs/file.c: remove sanity_check and add likely/unlikely in alloc_fd()

next inclusion
category: performance
bugzilla: https://gitee.com/src-openeuler/kernel/issues/IB1S01

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/?id=52732bb9abc9ee5b82ed62edef51be4a255fc78a



--------------------------------

alloc_fd() has a sanity check inside to make sure the struct file mapping to the
allocated fd is NULL. Remove this sanity check since it can be assured by
exisitng zero initialization and NULL set when recycling fd. Meanwhile, add
likely/unlikely and expand_file() call avoidance to reduce the work under
file_lock.

Reviewed-by: default avatarJan Kara <jack@suse.cz>
Reviewed-by: default avatarTim Chen <tim.c.chen@linux.intel.com>
Signed-off-by: default avatarYu Ma <yu.ma@intel.com>
Link: https://lore.kernel.org/r/20240717145018.3972922-2-yu.ma@intel.com


Signed-off-by: default avatarChristian Brauner <brauner@kernel.org>
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
Conflicts:
	fs/file.c
[Context conflict]
Signed-off-by: default avatarJinjie Ruan <ruanjinjie@huawei.com>
parent 34a9039b
Loading
Loading
Loading
Loading
+15 −19
Original line number Diff line number Diff line
@@ -517,7 +517,7 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags)
	if (fd < files->next_fd)
		fd = files->next_fd;

	if (fd < fdt->max_fds)
	if (likely(fd < fdt->max_fds))
		fd = find_next_fd(fdt, fd);

	/*
@@ -525,9 +525,10 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags)
	 * will limit the total number of files that can be opened.
	 */
	error = -EMFILE;
	if (fd >= end)
	if (unlikely(fd >= end))
		goto out;

	if (unlikely(fd >= fdt->max_fds)) {
		error = expand_files(files, fd);
		if (error < 0)
			goto out;
@@ -538,6 +539,8 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags)
		 */
		if (error)
			goto repeat;
	}

	if (files_cg_alloc_fd(files, 1)) {
		error = -EMFILE;
		goto out;
@@ -552,13 +555,6 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags)
	else
		__clear_close_on_exec(fd, fdt);
	error = fd;
#if 1
	/* Sanity check */
	if (rcu_access_pointer(fdt->fd[fd]) != NULL) {
		printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
		rcu_assign_pointer(fdt->fd[fd], NULL);
	}
#endif

out:
	spin_unlock(&files->file_lock);
@@ -623,7 +619,7 @@ void fd_install(unsigned int fd, struct file *file)
		rcu_read_unlock_sched();
		spin_lock(&files->file_lock);
		fdt = files_fdtable(files);
		BUG_ON(fdt->fd[fd] != NULL);
		WARN_ON(fdt->fd[fd] != NULL);
		rcu_assign_pointer(fdt->fd[fd], file);
		spin_unlock(&files->file_lock);
		return;