Skip to content

Commit dff745c

Browse files
amir73ilbrauner
authored andcommitted
fs: move cleanup from init_file() into its callers
The use of file_free_rcu() in init_file() to free the struct that was allocated by the caller was hacky and we got what we deserved. Let init_file() and its callers take care of cleaning up each after their own allocated resources on error. Fixes: 62d53c4 ("fs: use backing_file container for internal files with "fake" f_path") # mainline only Reported-and-tested-by: [email protected] Signed-off-by: Amir Goldstein <[email protected]> Message-Id: <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 995b406 commit dff745c

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

fs/file_table.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static int init_file(struct file *f, int flags, const struct cred *cred)
160160
f->f_cred = get_cred(cred);
161161
error = security_file_alloc(f);
162162
if (unlikely(error)) {
163-
file_free_rcu(&f->f_rcuhead);
163+
put_cred(f->f_cred);
164164
return error;
165165
}
166166

@@ -208,8 +208,10 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
208208
return ERR_PTR(-ENOMEM);
209209

210210
error = init_file(f, flags, cred);
211-
if (unlikely(error))
211+
if (unlikely(error)) {
212+
kmem_cache_free(filp_cachep, f);
212213
return ERR_PTR(error);
214+
}
213215

214216
percpu_counter_inc(&nr_files);
215217

@@ -240,8 +242,10 @@ struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred)
240242
return ERR_PTR(-ENOMEM);
241243

242244
error = init_file(f, flags, cred);
243-
if (unlikely(error))
245+
if (unlikely(error)) {
246+
kmem_cache_free(filp_cachep, f);
244247
return ERR_PTR(error);
248+
}
245249

246250
f->f_mode |= FMODE_NOACCOUNT;
247251

@@ -265,8 +269,10 @@ struct file *alloc_empty_backing_file(int flags, const struct cred *cred)
265269
return ERR_PTR(-ENOMEM);
266270

267271
error = init_file(&ff->file, flags, cred);
268-
if (unlikely(error))
272+
if (unlikely(error)) {
273+
kfree(ff);
269274
return ERR_PTR(error);
275+
}
270276

271277
ff->file.f_mode |= FMODE_BACKING | FMODE_NOACCOUNT;
272278
return &ff->file;

0 commit comments

Comments
 (0)