Skip to content

Commit 1c94984

Browse files
VillemoesAl Viro
authored andcommitted
vfs: make sure struct filename->iname is word-aligned
I noticed that offsetof(struct filename, iname) is actually 28 on 64 bit platforms, so we always pass an unaligned pointer to strncpy_from_user. This is mostly a problem for those 64 bit platforms without HAVE_EFFICIENT_UNALIGNED_ACCESS, but even on x86_64, unaligned accesses carry a penalty. A user-space microbenchmark doing nothing but strncpy_from_user from the same (aligned) source string runs about 5% faster when the destination is aligned. That number increases to 20% when the string is long enough (~32 bytes) that we cross a cache line boundary - that's for example the case for about half the files a "git status" in a kernel tree ends up stat'ing. This won't make any real-life workloads 5%, or even 1%, faster, but path lookup is common enough that cutting even a few cycles should be worthwhile. So ensure we always pass an aligned destination pointer to strncpy_from_user. Instead of explicit padding, simply swap the refcnt and aname members, as suggested by Al Viro. Signed-off-by: Rasmus Villemoes <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent 304ec48 commit 1c94984

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

fs/namei.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <linux/bitops.h>
4040
#include <linux/init_task.h>
4141
#include <linux/uaccess.h>
42+
#include <linux/build_bug.h>
4243

4344
#include "internal.h"
4445
#include "mount.h"
@@ -130,6 +131,7 @@ getname_flags(const char __user *filename, int flags, int *empty)
130131
struct filename *result;
131132
char *kname;
132133
int len;
134+
BUILD_BUG_ON(offsetof(struct filename, iname) % sizeof(long) != 0);
133135

134136
result = audit_reusename(filename);
135137
if (result)

include/linux/fs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2380,8 +2380,8 @@ struct audit_names;
23802380
struct filename {
23812381
const char *name; /* pointer to actual string */
23822382
const __user char *uptr; /* original userland pointer */
2383-
struct audit_names *aname;
23842383
int refcnt;
2384+
struct audit_names *aname;
23852385
const char iname[];
23862386
};
23872387

0 commit comments

Comments
 (0)