From: Al Viro <viro@zeniv.linux.org.uk>
To: linux-fsdevel@vger.kernel.org
Cc: torvalds@linux-foundation.org, brauner@kernel.org, jack@suse.cz,
mjguzik@gmail.com, paul@paul-moore.com, axboe@kernel.dk,
audit@vger.kernel.org, io-uring@vger.kernel.org
Subject: [RFC PATCH v2 14/18] struct filename: use names_cachep only for getname() and friends
Date: Sat, 29 Nov 2025 17:01:38 +0000 [thread overview]
Message-ID: <20251129170142.150639-15-viro@zeniv.linux.org.uk> (raw)
In-Reply-To: <20251129170142.150639-1-viro@zeniv.linux.org.uk>
Instances of struct filename come from names_cachep (via
__getname()). That is done by getname_flags() and getname_kernel()
and these two are the main callers of __getname(). However, there are
other callers that simply want to allocate PATH_MAX bytes for uses that
have nothing to do with struct filename.
We want saner allocation rules for long pathnames, so that struct
filename would *always* come from names_cachep, with the out-of-line
pathname getting kmalloc'ed. For that we need to be able to change the
size of objects allocated by getname_flags()/getname_kernel().
That requires the rest of __getname() users to stop using
names_cachep; we could explicitly switch all of those to kmalloc(),
but that would cause quite a bit of noise. So the plan is to switch
getname_...() to new helpers and turn __getname() into a wrapper for
kmalloc(). Remaining __getname() users could be converted to explicit
kmalloc() at leisure, hopefully along with figuring out what size do
they really want - PATH_MAX is an overkill for some of them, used out
of laziness ("we have a convenient helper that does 4K allocations and
that's large enough, let's use it").
As a side benefit, names_cachep is no longer used outside
of fs/namei.c, so we can move it there and be done with that.
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
---
fs/dcache.c | 8 +-------
fs/internal.h | 2 ++
fs/namei.c | 37 ++++++++++++++++++++++++++++---------
include/linux/fs.h | 6 ++----
4 files changed, 33 insertions(+), 20 deletions(-)
diff --git a/fs/dcache.c b/fs/dcache.c
index 035cccbc9276..761283f13200 100644
--- a/fs/dcache.c
+++ b/fs/dcache.c
@@ -3246,10 +3246,6 @@ static void __init dcache_init(void)
runtime_const_init(ptr, dentry_hashtable);
}
-/* SLAB cache for __getname() consumers */
-struct kmem_cache *names_cachep __ro_after_init;
-EXPORT_SYMBOL(names_cachep);
-
void __init vfs_caches_init_early(void)
{
int i;
@@ -3263,9 +3259,7 @@ void __init vfs_caches_init_early(void)
void __init vfs_caches_init(void)
{
- names_cachep = kmem_cache_create_usercopy("names_cache", PATH_MAX, 0,
- SLAB_HWCACHE_ALIGN|SLAB_PANIC, 0, PATH_MAX, NULL);
-
+ filename_init();
dcache_init();
inode_init();
files_init();
diff --git a/fs/internal.h b/fs/internal.h
index 9b2b4d116880..e16e72b246c2 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -68,6 +68,8 @@ int vfs_tmpfile(struct mnt_idmap *idmap,
struct file *file, umode_t mode);
struct dentry *d_hash_and_lookup(struct dentry *, struct qstr *);
+void __init filename_init(void);
+
/*
* namespace.c
*/
diff --git a/fs/namei.c b/fs/namei.c
index 62e992e4f152..cf7d8608ce4e 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -125,6 +125,25 @@
#define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
+/* SLAB cache for struct filename instances */
+static struct kmem_cache *names_cachep __ro_after_init;
+
+void __init filename_init(void)
+{
+ names_cachep = kmem_cache_create_usercopy("names_cache", PATH_MAX, 0,
+ SLAB_HWCACHE_ALIGN|SLAB_PANIC, 0, PATH_MAX, NULL);
+}
+
+static inline struct filename *alloc_filename(void)
+{
+ return kmem_cache_alloc(names_cachep, GFP_KERNEL);
+}
+
+static inline void free_filename(struct filename *p)
+{
+ kmem_cache_free(names_cachep, p);
+}
+
static inline void initname(struct filename *name)
{
name->aname = NULL;
@@ -164,7 +183,7 @@ getname_flags(const char __user *filename, int flags)
char *kname;
int len;
- result = __getname();
+ result = alloc_filename();
if (unlikely(!result))
return ERR_PTR(-ENOMEM);
@@ -181,13 +200,13 @@ getname_flags(const char __user *filename, int flags)
*/
if (unlikely(len <= 0)) {
if (unlikely(len < 0)) {
- __putname(result);
+ free_filename(result);
return ERR_PTR(len);
}
/* The empty path is special. */
if (!(flags & LOOKUP_EMPTY)) {
- __putname(result);
+ free_filename(result);
return ERR_PTR(-ENOENT);
}
}
@@ -201,7 +220,7 @@ getname_flags(const char __user *filename, int flags)
if (unlikely(len == EMBEDDED_NAME_MAX)) {
struct filename *p = getname_long(result, filename);
if (IS_ERR(p)) {
- __putname(result);
+ free_filename(result);
return p;
}
result = p;
@@ -242,7 +261,7 @@ struct filename *getname_kernel(const char * filename)
struct filename *result;
int len = strlen(filename) + 1;
- result = __getname();
+ result = alloc_filename();
if (unlikely(!result))
return ERR_PTR(-ENOMEM);
@@ -254,13 +273,13 @@ struct filename *getname_kernel(const char * filename)
tmp = kmalloc(size, GFP_KERNEL);
if (unlikely(!tmp)) {
- __putname(result);
+ free_filename(result);
return ERR_PTR(-ENOMEM);
}
tmp->name = (char *)result;
result = tmp;
} else {
- __putname(result);
+ free_filename(result);
return ERR_PTR(-ENAMETOOLONG);
}
memcpy((char *)result->name, filename, len);
@@ -287,10 +306,10 @@ void putname(struct filename *name)
}
if (name->name != name->iname) {
- __putname(name->name);
+ free_filename((struct filename *)name->name);
kfree(name);
} else
- __putname(name);
+ free_filename(name);
}
EXPORT_SYMBOL(putname);
diff --git a/include/linux/fs.h b/include/linux/fs.h
index bbae3cfdc338..59c5c67985ab 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2957,10 +2957,8 @@ static inline int finish_open_simple(struct file *file, int error)
extern void __init vfs_caches_init_early(void);
extern void __init vfs_caches_init(void);
-extern struct kmem_cache *names_cachep;
-
-#define __getname() kmem_cache_alloc(names_cachep, GFP_KERNEL)
-#define __putname(name) kmem_cache_free(names_cachep, (void *)(name))
+#define __getname() kmalloc(PATH_MAX, GFP_KERNEL)
+#define __putname(name) kfree(name)
extern struct super_block *blockdev_superblock;
static inline bool sb_is_blkdev_sb(struct super_block *sb)
--
2.47.3
next prev parent reply other threads:[~2025-11-29 17:01 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-29 17:01 [RFC PATCH v2 00/18] io_uring, struct filename and audit Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 01/18] do_faccessat(): import pathname only once Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 02/18] do_fchmodat(): " Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 03/18] do_fchownat(): " Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 04/18] do_utimes_path(): " Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 05/18] chdir(2): " Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 06/18] chroot(2): " Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 07/18] user_statfs(): " Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 08/18] do_sys_truncate(): " Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 09/18] do_readlinkat(): " Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 10/18] get rid of audit_reusename() Al Viro
2025-12-16 2:14 ` Paul Moore
2025-11-29 17:01 ` [RFC PATCH v2 11/18] ntfs: ->d_compare() must not block Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 12/18] getname_flags() massage, part 1 Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 13/18] getname_flags() massage, part 2 Al Viro
2025-11-29 17:01 ` Al Viro [this message]
2025-11-29 17:01 ` [RFC PATCH v2 15/18] struct filename: saner handling of long names Al Viro
2025-11-29 17:33 ` Mateusz Guzik
2025-11-30 4:06 ` Al Viro
2025-11-30 4:38 ` Mateusz Guzik
2025-11-29 17:01 ` [RFC PATCH v2 16/18] allow incomplete imports of filenames Al Viro
2025-11-29 17:01 ` [RFC PATCH v2 17/18] fs: touch up predicts in putname() Al Viro
2025-11-29 17:34 ` Mateusz Guzik
2025-11-29 17:01 ` [RFC PATCH v2 18/18] struct filename ->refcnt doesn't need to be atomic Al Viro
2025-12-16 2:18 ` Paul Moore
2025-12-10 1:31 ` [RFC PATCH v2 00/18] io_uring, struct filename and audit Jens Axboe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251129170142.150639-15-viro@zeniv.linux.org.uk \
--to=viro@zeniv.linux.org.uk \
--cc=audit@vger.kernel.org \
--cc=axboe@kernel.dk \
--cc=brauner@kernel.org \
--cc=io-uring@vger.kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=mjguzik@gmail.com \
--cc=paul@paul-moore.com \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox