struct filename: use names_cachep only for getname() and friends

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>
This commit is contained in:
Al Viro
2025-11-19 19:19:24 -05:00
parent 8f2ac84817
commit c3a3577cdb
4 changed files with 33 additions and 20 deletions

View File

@@ -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 (unlikely(name->name != name->iname)) {
__putname(name->name);
free_filename((struct filename *)name->name);
kfree(name);
} else
__putname(name);
free_filename(name);
}
EXPORT_SYMBOL(putname);