Skip to content

Commit 5aefe53

Browse files
author
Ian Kent
committed
fs/file.c: remove sanity_check and add likely/unlikely in alloc_fd()
JIRA: https://issues.redhat.com/browse/RHEL-38569 Upstream status: Linus commit 52732bb Author: Yu Ma <yu.ma@intel.com> Date: Wed Jul 17 10:50:16 2024 -0400 fs/file.c: remove sanity_check and add likely/unlikely in alloc_fd() 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 initilization and NULL set when recycling fd. Meanwhile, add likely/unlikely and expand_file() call avoidance to reduce the work under file_lock. Reviewed-by: Jan Kara <jack@suse.cz> Reviewed-by: Tim Chen <tim.c.chen@linux.intel.com> Signed-off-by: Yu Ma <yu.ma@intel.com> Link: https://lore.kernel.org/r/20240717145018.3972922-2-yu.ma@intel.com Signed-off-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Ian Kent <ikent@redhat.com>
1 parent 6a6fa8e commit 5aefe53

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

fs/file.c

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -559,27 +559,29 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags)
559559
if (fd < files->next_fd)
560560
fd = files->next_fd;
561561

562-
if (fd < fdt->max_fds)
562+
if (likely(fd < fdt->max_fds))
563563
fd = find_next_fd(fdt, fd);
564564

565565
/*
566566
* N.B. For clone tasks sharing a files structure, this test
567567
* will limit the total number of files that can be opened.
568568
*/
569569
error = -EMFILE;
570-
if (fd >= end)
570+
if (unlikely(fd >= end))
571571
goto out;
572572

573-
error = expand_files(files, fd);
574-
if (error < 0)
575-
goto out;
573+
if (unlikely(fd >= fdt->max_fds)) {
574+
error = expand_files(files, fd);
575+
if (error < 0)
576+
goto out;
576577

577-
/*
578-
* If we needed to expand the fs array we
579-
* might have blocked - try again.
580-
*/
581-
if (error)
582-
goto repeat;
578+
/*
579+
* If we needed to expand the fs array we
580+
* might have blocked - try again.
581+
*/
582+
if (error)
583+
goto repeat;
584+
}
583585

584586
if (start <= files->next_fd)
585587
files->next_fd = fd + 1;
@@ -590,13 +592,6 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags)
590592
else
591593
__clear_close_on_exec(fd, fdt);
592594
error = fd;
593-
#if 1
594-
/* Sanity check */
595-
if (rcu_access_pointer(fdt->fd[fd]) != NULL) {
596-
printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
597-
rcu_assign_pointer(fdt->fd[fd], NULL);
598-
}
599-
#endif
600595

601596
out:
602597
spin_unlock(&files->file_lock);
@@ -662,7 +657,7 @@ void fd_install(unsigned int fd, struct file *file)
662657
rcu_read_unlock_sched();
663658
spin_lock(&files->file_lock);
664659
fdt = files_fdtable(files);
665-
BUG_ON(fdt->fd[fd] != NULL);
660+
WARN_ON(fdt->fd[fd] != NULL);
666661
rcu_assign_pointer(fdt->fd[fd], file);
667662
spin_unlock(&files->file_lock);
668663
return;

0 commit comments

Comments
 (0)