Skip to content

Commit 4647ea5

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-38570 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 5b54e29 commit 4647ea5

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
@@ -481,27 +481,29 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags)
481481
if (fd < files->next_fd)
482482
fd = files->next_fd;
483483

484-
if (fd < fdt->max_fds)
484+
if (likely(fd < fdt->max_fds))
485485
fd = find_next_fd(fdt, fd);
486486

487487
/*
488488
* N.B. For clone tasks sharing a files structure, this test
489489
* will limit the total number of files that can be opened.
490490
*/
491491
error = -EMFILE;
492-
if (fd >= end)
492+
if (unlikely(fd >= end))
493493
goto out;
494494

495-
error = expand_files(files, fd);
496-
if (error < 0)
497-
goto out;
495+
if (unlikely(fd >= fdt->max_fds)) {
496+
error = expand_files(files, fd);
497+
if (error < 0)
498+
goto out;
498499

499-
/*
500-
* If we needed to expand the fs array we
501-
* might have blocked - try again.
502-
*/
503-
if (error)
504-
goto repeat;
500+
/*
501+
* If we needed to expand the fs array we
502+
* might have blocked - try again.
503+
*/
504+
if (error)
505+
goto repeat;
506+
}
505507

506508
if (start <= files->next_fd)
507509
files->next_fd = fd + 1;
@@ -512,13 +514,6 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags)
512514
else
513515
__clear_close_on_exec(fd, fdt);
514516
error = fd;
515-
#if 1
516-
/* Sanity check */
517-
if (rcu_access_pointer(fdt->fd[fd]) != NULL) {
518-
printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
519-
rcu_assign_pointer(fdt->fd[fd], NULL);
520-
}
521-
#endif
522517

523518
out:
524519
spin_unlock(&files->file_lock);
@@ -581,7 +576,7 @@ void fd_install(unsigned int fd, struct file *file)
581576
rcu_read_unlock_sched();
582577
spin_lock(&files->file_lock);
583578
fdt = files_fdtable(files);
584-
BUG_ON(fdt->fd[fd] != NULL);
579+
WARN_ON(fdt->fd[fd] != NULL);
585580
rcu_assign_pointer(fdt->fd[fd], file);
586581
spin_unlock(&files->file_lock);
587582
return;

0 commit comments

Comments
 (0)