Skip to content

Commit 1f1651d

Browse files
mjguzikbrauner
authored andcommitted
fs: hide file and bfile caches behind runtime const machinery
s/cachep/cache/ for consistency with namei and dentry caches. Signed-off-by: Mateusz Guzik <mjguzik@gmail.com> Link: https://patch.msgid.link/20260328173728.3388070-1-mjguzik@gmail.com Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 18f2e0e commit 1f1651d

3 files changed

Lines changed: 23 additions & 14 deletions

File tree

fs/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static struct fdtable *alloc_fdtable(unsigned int slots_wanted)
200200
/*
201201
* Check if the allocation size would exceed INT_MAX. kvmalloc_array()
202202
* and kvmalloc() will warn if the allocation size is greater than
203-
* INT_MAX, as filp_cachep objects are not __GFP_NOWARN.
203+
* INT_MAX, as filp_cache objects are not __GFP_NOWARN.
204204
*
205205
* This can happen when sysctl_nr_open is set to a very high value and
206206
* a process tries to use a file descriptor near that limit. For example,

fs/file_table.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
#include <linux/atomic.h>
3232

33+
#include <asm/runtime-const.h>
34+
3335
#include "internal.h"
3436

3537
/* sysctl tunables... */
@@ -38,8 +40,10 @@ static struct files_stat_struct files_stat = {
3840
};
3941

4042
/* SLAB cache for file structures */
41-
static struct kmem_cache *filp_cachep __ro_after_init;
42-
static struct kmem_cache *bfilp_cachep __ro_after_init;
43+
static struct kmem_cache *__filp_cache __ro_after_init;
44+
#define filp_cache runtime_const_ptr(__filp_cache)
45+
static struct kmem_cache *__bfilp_cache __ro_after_init;
46+
#define bfilp_cache runtime_const_ptr(__bfilp_cache)
4347

4448
static struct percpu_counter nr_files __cacheline_aligned_in_smp;
4549

@@ -74,9 +78,9 @@ static inline void file_free(struct file *f)
7478
put_cred(f->f_cred);
7579
if (unlikely(f->f_mode & FMODE_BACKING)) {
7680
path_put(backing_file_user_path(f));
77-
kmem_cache_free(bfilp_cachep, backing_file(f));
81+
kmem_cache_free(bfilp_cache, backing_file(f));
7882
} else {
79-
kmem_cache_free(filp_cachep, f);
83+
kmem_cache_free(filp_cache, f);
8084
}
8185
}
8286

@@ -234,13 +238,13 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
234238
goto over;
235239
}
236240

237-
f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
241+
f = kmem_cache_alloc(filp_cache, GFP_KERNEL);
238242
if (unlikely(!f))
239243
return ERR_PTR(-ENOMEM);
240244

241245
error = init_file(f, flags, cred);
242246
if (unlikely(error)) {
243-
kmem_cache_free(filp_cachep, f);
247+
kmem_cache_free(filp_cache, f);
244248
return ERR_PTR(error);
245249
}
246250

@@ -268,13 +272,13 @@ struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred)
268272
struct file *f;
269273
int error;
270274

271-
f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
275+
f = kmem_cache_alloc(filp_cache, GFP_KERNEL);
272276
if (unlikely(!f))
273277
return ERR_PTR(-ENOMEM);
274278

275279
error = init_file(f, flags, cred);
276280
if (unlikely(error)) {
277-
kmem_cache_free(filp_cachep, f);
281+
kmem_cache_free(filp_cache, f);
278282
return ERR_PTR(error);
279283
}
280284

@@ -295,13 +299,13 @@ struct file *alloc_empty_backing_file(int flags, const struct cred *cred)
295299
struct backing_file *ff;
296300
int error;
297301

298-
ff = kmem_cache_alloc(bfilp_cachep, GFP_KERNEL);
302+
ff = kmem_cache_alloc(bfilp_cache, GFP_KERNEL);
299303
if (unlikely(!ff))
300304
return ERR_PTR(-ENOMEM);
301305

302306
error = init_file(&ff->file, flags, cred);
303307
if (unlikely(error)) {
304-
kmem_cache_free(bfilp_cachep, ff);
308+
kmem_cache_free(bfilp_cache, ff);
305309
return ERR_PTR(error);
306310
}
307311

@@ -593,14 +597,17 @@ void __init files_init(void)
593597
.freeptr_offset = offsetof(struct file, f_freeptr),
594598
};
595599

596-
filp_cachep = kmem_cache_create("filp", sizeof(struct file), &args,
600+
__filp_cache = kmem_cache_create("filp", sizeof(struct file), &args,
597601
SLAB_HWCACHE_ALIGN | SLAB_PANIC |
598602
SLAB_ACCOUNT | SLAB_TYPESAFE_BY_RCU);
603+
runtime_const_init(ptr, __filp_cache);
599604

600605
args.freeptr_offset = offsetof(struct backing_file, bf_freeptr);
601-
bfilp_cachep = kmem_cache_create("bfilp", sizeof(struct backing_file),
606+
__bfilp_cache = kmem_cache_create("bfilp", sizeof(struct backing_file),
602607
&args, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
603608
SLAB_ACCOUNT | SLAB_TYPESAFE_BY_RCU);
609+
runtime_const_init(ptr, __bfilp_cache);
610+
604611
percpu_counter_init(&nr_files, 0, GFP_KERNEL);
605612
}
606613

include/asm-generic/vmlinux.lds.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,9 @@
973973
RUNTIME_CONST(shift, d_hash_shift) \
974974
RUNTIME_CONST(ptr, dentry_hashtable) \
975975
RUNTIME_CONST(ptr, __dentry_cache) \
976-
RUNTIME_CONST(ptr, __names_cache)
976+
RUNTIME_CONST(ptr, __names_cache) \
977+
RUNTIME_CONST(ptr, __filp_cache) \
978+
RUNTIME_CONST(ptr, __bfilp_cache)
977979

978980
/* Alignment must be consistent with (kunit_suite *) in include/kunit/test.h */
979981
#define KUNIT_TABLE() \

0 commit comments

Comments
 (0)