Skip to content

Commit 52732bb

Browse files
intelmyAl Viro
authored andcommitted
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 <[email protected]> Reviewed-by: Tim Chen <[email protected]> Signed-off-by: Yu Ma <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent cab0515 commit 52732bb

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
@@ -496,27 +496,29 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags)
496496
if (fd < files->next_fd)
497497
fd = files->next_fd;
498498

499-
if (fd < fdt->max_fds)
499+
if (likely(fd < fdt->max_fds))
500500
fd = find_next_fd(fdt, fd);
501501

502502
/*
503503
* N.B. For clone tasks sharing a files structure, this test
504504
* will limit the total number of files that can be opened.
505505
*/
506506
error = -EMFILE;
507-
if (fd >= end)
507+
if (unlikely(fd >= end))
508508
goto out;
509509

510-
error = expand_files(files, fd);
511-
if (error < 0)
512-
goto out;
510+
if (unlikely(fd >= fdt->max_fds)) {
511+
error = expand_files(files, fd);
512+
if (error < 0)
513+
goto out;
513514

514-
/*
515-
* If we needed to expand the fs array we
516-
* might have blocked - try again.
517-
*/
518-
if (error)
519-
goto repeat;
515+
/*
516+
* If we needed to expand the fs array we
517+
* might have blocked - try again.
518+
*/
519+
if (error)
520+
goto repeat;
521+
}
520522

521523
if (start <= files->next_fd)
522524
files->next_fd = fd + 1;
@@ -527,13 +529,6 @@ static int alloc_fd(unsigned start, unsigned end, unsigned flags)
527529
else
528530
__clear_close_on_exec(fd, fdt);
529531
error = fd;
530-
#if 1
531-
/* Sanity check */
532-
if (rcu_access_pointer(fdt->fd[fd]) != NULL) {
533-
printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
534-
rcu_assign_pointer(fdt->fd[fd], NULL);
535-
}
536-
#endif
537532

538533
out:
539534
spin_unlock(&files->file_lock);
@@ -599,7 +594,7 @@ void fd_install(unsigned int fd, struct file *file)
599594
rcu_read_unlock_sched();
600595
spin_lock(&files->file_lock);
601596
fdt = files_fdtable(files);
602-
BUG_ON(fdt->fd[fd] != NULL);
597+
WARN_ON(fdt->fd[fd] != NULL);
603598
rcu_assign_pointer(fdt->fd[fd], file);
604599
spin_unlock(&files->file_lock);
605600
return;

0 commit comments

Comments
 (0)