Skip to content

Commit 7cb537b

Browse files
Al Virobrauner
authored andcommitted
file: massage cleanup of files that failed to open
A file that has never gotten FMODE_OPENED will never have RCU-accessed references, its final fput() is equivalent to file_free() and if it doesn't have FMODE_BACKING either, it can be done from any context and won't need task_work treatment. Now that we have SLAB_TYPESAFE_BY_RCU we can simplify this and have other callers benefit. All of that can be achieved easier is to make fput() recoginze that case and call file_free() directly. No need to introduce a special primitive for that. It also allowed things like failing dentry_open() could benefit from that as well. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> [Christian Brauner <brauner@kernel.org>: massage commit message] Link: https://lore.kernel.org/r/20231126020834.GC38156@ZenIV Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent b85ea95 commit 7cb537b

3 files changed

Lines changed: 5 additions & 17 deletions

File tree

fs/file_table.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,6 @@ static inline void file_free(struct file *f)
7575
}
7676
}
7777

78-
void release_empty_file(struct file *f)
79-
{
80-
WARN_ON_ONCE(f->f_mode & (FMODE_BACKING | FMODE_OPENED));
81-
if (atomic_long_dec_and_test(&f->f_count)) {
82-
security_file_free(f);
83-
put_cred(f->f_cred);
84-
if (likely(!(f->f_mode & FMODE_NOACCOUNT)))
85-
percpu_counter_dec(&nr_files);
86-
kmem_cache_free(filp_cachep, f);
87-
}
88-
}
89-
9078
/*
9179
* Return the total number of open files in the system
9280
*/
@@ -445,6 +433,10 @@ void fput(struct file *file)
445433
if (atomic_long_dec_and_test(&file->f_count)) {
446434
struct task_struct *task = current;
447435

436+
if (unlikely(!(file->f_mode & (FMODE_BACKING | FMODE_OPENED)))) {
437+
file_free(file);
438+
return;
439+
}
448440
if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
449441
init_task_work(&file->f_rcuhead, ____fput);
450442
if (!task_work_add(task, &file->f_rcuhead, TWA_RESUME))

fs/internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ extern void chroot_fs_refs(const struct path *, const struct path *);
9494
struct file *alloc_empty_file(int flags, const struct cred *cred);
9595
struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred);
9696
struct file *alloc_empty_backing_file(int flags, const struct cred *cred);
97-
void release_empty_file(struct file *f);
9897

9998
static inline void file_put_write_access(struct file *file)
10099
{

fs/namei.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3785,10 +3785,7 @@ static struct file *path_openat(struct nameidata *nd,
37853785
WARN_ON(1);
37863786
error = -EINVAL;
37873787
}
3788-
if (unlikely(file->f_mode & FMODE_OPENED))
3789-
fput(file);
3790-
else
3791-
release_empty_file(file);
3788+
fput(file);
37923789
if (error == -EOPENSTALE) {
37933790
if (flags & LOOKUP_RCU)
37943791
error = -ECHILD;

0 commit comments

Comments
 (0)