mirror of https://gitee.com/openkylin/linux.git
vfs: embed struct filename inside of names_cache allocation if possible
In the common case where a name is much smaller than PATH_MAX, an extra allocation for struct filename is unnecessary. Before allocating a separate one, try to embed the struct filename inside the buffer first. If it turns out that that's not long enough, then fall back to allocating a separate struct filename and redoing the copy. Signed-off-by: Jeff Layton <jlayton@redhat.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
This commit is contained in:
parent
adb5c2473d
commit
7950e3852a
69
fs/namei.c
69
fs/namei.c
|
@ -119,40 +119,69 @@
|
||||||
*/
|
*/
|
||||||
void final_putname(struct filename *name)
|
void final_putname(struct filename *name)
|
||||||
{
|
{
|
||||||
__putname(name->name);
|
if (name->separate) {
|
||||||
kfree(name);
|
__putname(name->name);
|
||||||
|
kfree(name);
|
||||||
|
} else {
|
||||||
|
__putname(name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
|
||||||
|
|
||||||
static struct filename *
|
static struct filename *
|
||||||
getname_flags(const char __user *filename, int flags, int *empty)
|
getname_flags(const char __user *filename, int flags, int *empty)
|
||||||
{
|
{
|
||||||
struct filename *result, *err;
|
struct filename *result, *err;
|
||||||
char *kname;
|
|
||||||
int len;
|
int len;
|
||||||
|
long max;
|
||||||
|
char *kname;
|
||||||
|
|
||||||
result = audit_reusename(filename);
|
result = audit_reusename(filename);
|
||||||
if (result)
|
if (result)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
/* FIXME: create dedicated slabcache? */
|
result = __getname();
|
||||||
result = kzalloc(sizeof(*result), GFP_KERNEL);
|
|
||||||
if (unlikely(!result))
|
if (unlikely(!result))
|
||||||
return ERR_PTR(-ENOMEM);
|
return ERR_PTR(-ENOMEM);
|
||||||
|
|
||||||
kname = __getname();
|
/*
|
||||||
if (unlikely(!kname)) {
|
* First, try to embed the struct filename inside the names_cache
|
||||||
err = ERR_PTR(-ENOMEM);
|
* allocation
|
||||||
goto error_free_name;
|
*/
|
||||||
}
|
kname = (char *)result + sizeof(*result);
|
||||||
|
|
||||||
result->name = kname;
|
result->name = kname;
|
||||||
result->uptr = filename;
|
result->separate = false;
|
||||||
len = strncpy_from_user(kname, filename, PATH_MAX);
|
max = EMBEDDED_NAME_MAX;
|
||||||
|
|
||||||
|
recopy:
|
||||||
|
len = strncpy_from_user(kname, filename, max);
|
||||||
if (unlikely(len < 0)) {
|
if (unlikely(len < 0)) {
|
||||||
err = ERR_PTR(len);
|
err = ERR_PTR(len);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
|
||||||
|
* separate struct filename so we can dedicate the entire
|
||||||
|
* names_cache allocation for the pathname, and re-do the copy from
|
||||||
|
* userland.
|
||||||
|
*/
|
||||||
|
if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
|
||||||
|
kname = (char *)result;
|
||||||
|
|
||||||
|
result = kzalloc(sizeof(*result), GFP_KERNEL);
|
||||||
|
if (!result) {
|
||||||
|
err = ERR_PTR(-ENOMEM);
|
||||||
|
result = (struct filename *)kname;
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
result->name = kname;
|
||||||
|
result->separate = true;
|
||||||
|
max = PATH_MAX;
|
||||||
|
goto recopy;
|
||||||
|
}
|
||||||
|
|
||||||
/* The empty path is special. */
|
/* The empty path is special. */
|
||||||
if (unlikely(!len)) {
|
if (unlikely(!len)) {
|
||||||
if (empty)
|
if (empty)
|
||||||
|
@ -163,15 +192,15 @@ getname_flags(const char __user *filename, int flags, int *empty)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ERR_PTR(-ENAMETOOLONG);
|
err = ERR_PTR(-ENAMETOOLONG);
|
||||||
if (likely(len < PATH_MAX)) {
|
if (unlikely(len >= PATH_MAX))
|
||||||
audit_getname(result);
|
goto error;
|
||||||
return result;
|
|
||||||
}
|
result->uptr = filename;
|
||||||
|
audit_getname(result);
|
||||||
|
return result;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
__putname(kname);
|
final_putname(result);
|
||||||
error_free_name:
|
|
||||||
kfree(result);
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2201,6 +2201,7 @@ struct filename {
|
||||||
const char *name; /* pointer to actual string */
|
const char *name; /* pointer to actual string */
|
||||||
const __user char *uptr; /* original userland pointer */
|
const __user char *uptr; /* original userland pointer */
|
||||||
struct audit_names *aname;
|
struct audit_names *aname;
|
||||||
|
bool separate; /* should "name" be freed? */
|
||||||
};
|
};
|
||||||
|
|
||||||
extern int do_truncate(struct dentry *, loff_t start, unsigned int time_attrs,
|
extern int do_truncate(struct dentry *, loff_t start, unsigned int time_attrs,
|
||||||
|
|
Loading…
Reference in New Issue