Re: [PATCH v3] statx: optimize copy of struct statx to userspace

2017-03-12 Thread Eric Biggers
Hi David,

On Sun, Mar 12, 2017 at 09:11:53AM +, David Howells wrote:
> Eric Biggers  wrote:
> 
> > +static int cp_statx(const struct kstat *stat, struct statx __user *buffer)
> > ...
> > +   struct statx tmp;
> 
> This function needs to be "noinline" as tmp is big.
> 
> > -   return statx_set_result(, buffer);
> > +
> > +   return cp_statx(, buffer);
> 
> Can you leave it as statx_set_result rather than renaming it to cp_statx?
> 
> David

I agree with using 'noinline'.

But I think the name "cp_statx()" is better because it's consistent with the
existing functions in the file:

cp_old_stat()
cp_new_stat()
cp_new_stat64()

- Eric


Re: [PATCH v3] statx: optimize copy of struct statx to userspace

2017-03-12 Thread Eric Biggers
Hi David,

On Sun, Mar 12, 2017 at 09:11:53AM +, David Howells wrote:
> Eric Biggers  wrote:
> 
> > +static int cp_statx(const struct kstat *stat, struct statx __user *buffer)
> > ...
> > +   struct statx tmp;
> 
> This function needs to be "noinline" as tmp is big.
> 
> > -   return statx_set_result(, buffer);
> > +
> > +   return cp_statx(, buffer);
> 
> Can you leave it as statx_set_result rather than renaming it to cp_statx?
> 
> David

I agree with using 'noinline'.

But I think the name "cp_statx()" is better because it's consistent with the
existing functions in the file:

cp_old_stat()
cp_new_stat()
cp_new_stat64()

- Eric


Re: [PATCH v3] statx: optimize copy of struct statx to userspace

2017-03-12 Thread David Howells
Eric Biggers  wrote:

> +static int cp_statx(const struct kstat *stat, struct statx __user *buffer)
> ...
> + struct statx tmp;

This function needs to be "noinline" as tmp is big.

> - return statx_set_result(, buffer);
> +
> + return cp_statx(, buffer);

Can you leave it as statx_set_result rather than renaming it to cp_statx?

David


Re: [PATCH v3] statx: optimize copy of struct statx to userspace

2017-03-12 Thread David Howells
Eric Biggers  wrote:

> +static int cp_statx(const struct kstat *stat, struct statx __user *buffer)
> ...
> + struct statx tmp;

This function needs to be "noinline" as tmp is big.

> - return statx_set_result(, buffer);
> +
> + return cp_statx(, buffer);

Can you leave it as statx_set_result rather than renaming it to cp_statx?

David


[PATCH v3] statx: optimize copy of struct statx to userspace

2017-03-11 Thread Eric Biggers
From: Eric Biggers 

I found that statx() was significantly slower than stat().  As a
microbenchmark, I compared 10,000,000 invocations of fstat() on a tmpfs
file to the same with statx() passed a NULL path:

$ time ./stat_benchmark

real0m1.464s
user0m0.275s
sys 0m1.187s

$ time ./statx_benchmark

real0m5.530s
user0m0.281s
sys 0m5.247s

statx is expected to be a little slower than stat because struct statx
is larger than struct stat, but not by *that* much.  It turns out that
most of the overhead was in copying struct statx to userspace, mostly in
all the stac/clac instructions that got generated for each __put_user()
call.  (This was on x86_64, but some other architectures, e.g. arm64,
have something similar now too.)

stat() instead initializes its struct on the stack and copies it to
userspace with a single call to copy_to_user().  This turns out to be
much faster, and changing statx to do this makes it almost as fast as
stat:

$ time ./statx_benchmark

real0m1.624s
user0m0.270s
sys 0m1.354s

Implementation-wise, for zeroing the reserved fields, I chose to simply
start by zeroing the full struct with memset.  This makes it clear that
every byte copied to userspace is initialized, even implicit padding
bytes (though there are none currently).  In the scenarios I tested, it
also performed the same as a designated initializer.  Manually
initializing each field was still slightly faster, but would have been
more error-prone and less verifiable.

Signed-off-by: Eric Biggers 
---
 fs/stat.c | 73 +++
 1 file changed, 31 insertions(+), 42 deletions(-)

diff --git a/fs/stat.c b/fs/stat.c
index fa0be59340cc..3fbecbfa6975 100644
--- a/fs/stat.c
+++ b/fs/stat.c
@@ -509,46 +509,36 @@ SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, 
filename,
 }
 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
 
-static inline int __put_timestamp(struct timespec *kts,
- struct statx_timestamp __user *uts)
+static int cp_statx(const struct kstat *stat, struct statx __user *buffer)
 {
-   return (__put_user(kts->tv_sec, >tv_sec) ||
-   __put_user(kts->tv_nsec,>tv_nsec   ) ||
-   __put_user(0,   >__reserved));
-}
-
-/*
- * Set the statx results.
- */
-static long statx_set_result(struct kstat *stat, struct statx __user *buffer)
-{
-   uid_t uid = from_kuid_munged(current_user_ns(), stat->uid);
-   gid_t gid = from_kgid_munged(current_user_ns(), stat->gid);
-
-   if (__put_user(stat->result_mask,   >stx_mask   ) ||
-   __put_user(stat->mode,  >stx_mode   ) ||
-   __clear_user(>__spare0, sizeof(buffer->__spare0)) ||
-   __put_user(stat->nlink, >stx_nlink  ) ||
-   __put_user(uid, >stx_uid) ||
-   __put_user(gid, >stx_gid) ||
-   __put_user(stat->attributes,>stx_attributes ) ||
-   __put_user(stat->blksize,   >stx_blksize) ||
-   __put_user(MAJOR(stat->rdev),   >stx_rdev_major ) ||
-   __put_user(MINOR(stat->rdev),   >stx_rdev_minor ) ||
-   __put_user(MAJOR(stat->dev),>stx_dev_major  ) ||
-   __put_user(MINOR(stat->dev),>stx_dev_minor  ) ||
-   __put_timestamp(>atime,   >stx_atime  ) ||
-   __put_timestamp(>btime,   >stx_btime  ) ||
-   __put_timestamp(>ctime,   >stx_ctime  ) ||
-   __put_timestamp(>mtime,   >stx_mtime  ) ||
-   __put_user(stat->ino,   >stx_ino) ||
-   __put_user(stat->size,  >stx_size   ) ||
-   __put_user(stat->blocks,>stx_blocks ) ||
-   __clear_user(>__spare1, sizeof(buffer->__spare1)) ||
-   __clear_user(>__spare2, sizeof(buffer->__spare2)))
-   return -EFAULT;
-
-   return 0;
+   struct statx tmp;
+
+   memset(, 0, sizeof(tmp));
+
+   tmp.stx_mask = stat->result_mask;
+   tmp.stx_blksize = stat->blksize;
+   tmp.stx_attributes = stat->attributes;
+   tmp.stx_nlink = stat->nlink;
+   tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
+   tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
+   tmp.stx_mode = stat->mode;
+   tmp.stx_ino = stat->ino;
+   tmp.stx_size = stat->size;
+   tmp.stx_blocks = stat->blocks;
+   tmp.stx_atime.tv_sec = stat->atime.tv_sec;
+   tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
+   tmp.stx_btime.tv_sec = stat->btime.tv_sec;
+   tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
+   tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;

[PATCH v3] statx: optimize copy of struct statx to userspace

2017-03-11 Thread Eric Biggers
From: Eric Biggers 

I found that statx() was significantly slower than stat().  As a
microbenchmark, I compared 10,000,000 invocations of fstat() on a tmpfs
file to the same with statx() passed a NULL path:

$ time ./stat_benchmark

real0m1.464s
user0m0.275s
sys 0m1.187s

$ time ./statx_benchmark

real0m5.530s
user0m0.281s
sys 0m5.247s

statx is expected to be a little slower than stat because struct statx
is larger than struct stat, but not by *that* much.  It turns out that
most of the overhead was in copying struct statx to userspace, mostly in
all the stac/clac instructions that got generated for each __put_user()
call.  (This was on x86_64, but some other architectures, e.g. arm64,
have something similar now too.)

stat() instead initializes its struct on the stack and copies it to
userspace with a single call to copy_to_user().  This turns out to be
much faster, and changing statx to do this makes it almost as fast as
stat:

$ time ./statx_benchmark

real0m1.624s
user0m0.270s
sys 0m1.354s

Implementation-wise, for zeroing the reserved fields, I chose to simply
start by zeroing the full struct with memset.  This makes it clear that
every byte copied to userspace is initialized, even implicit padding
bytes (though there are none currently).  In the scenarios I tested, it
also performed the same as a designated initializer.  Manually
initializing each field was still slightly faster, but would have been
more error-prone and less verifiable.

Signed-off-by: Eric Biggers 
---
 fs/stat.c | 73 +++
 1 file changed, 31 insertions(+), 42 deletions(-)

diff --git a/fs/stat.c b/fs/stat.c
index fa0be59340cc..3fbecbfa6975 100644
--- a/fs/stat.c
+++ b/fs/stat.c
@@ -509,46 +509,36 @@ SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, 
filename,
 }
 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
 
-static inline int __put_timestamp(struct timespec *kts,
- struct statx_timestamp __user *uts)
+static int cp_statx(const struct kstat *stat, struct statx __user *buffer)
 {
-   return (__put_user(kts->tv_sec, >tv_sec) ||
-   __put_user(kts->tv_nsec,>tv_nsec   ) ||
-   __put_user(0,   >__reserved));
-}
-
-/*
- * Set the statx results.
- */
-static long statx_set_result(struct kstat *stat, struct statx __user *buffer)
-{
-   uid_t uid = from_kuid_munged(current_user_ns(), stat->uid);
-   gid_t gid = from_kgid_munged(current_user_ns(), stat->gid);
-
-   if (__put_user(stat->result_mask,   >stx_mask   ) ||
-   __put_user(stat->mode,  >stx_mode   ) ||
-   __clear_user(>__spare0, sizeof(buffer->__spare0)) ||
-   __put_user(stat->nlink, >stx_nlink  ) ||
-   __put_user(uid, >stx_uid) ||
-   __put_user(gid, >stx_gid) ||
-   __put_user(stat->attributes,>stx_attributes ) ||
-   __put_user(stat->blksize,   >stx_blksize) ||
-   __put_user(MAJOR(stat->rdev),   >stx_rdev_major ) ||
-   __put_user(MINOR(stat->rdev),   >stx_rdev_minor ) ||
-   __put_user(MAJOR(stat->dev),>stx_dev_major  ) ||
-   __put_user(MINOR(stat->dev),>stx_dev_minor  ) ||
-   __put_timestamp(>atime,   >stx_atime  ) ||
-   __put_timestamp(>btime,   >stx_btime  ) ||
-   __put_timestamp(>ctime,   >stx_ctime  ) ||
-   __put_timestamp(>mtime,   >stx_mtime  ) ||
-   __put_user(stat->ino,   >stx_ino) ||
-   __put_user(stat->size,  >stx_size   ) ||
-   __put_user(stat->blocks,>stx_blocks ) ||
-   __clear_user(>__spare1, sizeof(buffer->__spare1)) ||
-   __clear_user(>__spare2, sizeof(buffer->__spare2)))
-   return -EFAULT;
-
-   return 0;
+   struct statx tmp;
+
+   memset(, 0, sizeof(tmp));
+
+   tmp.stx_mask = stat->result_mask;
+   tmp.stx_blksize = stat->blksize;
+   tmp.stx_attributes = stat->attributes;
+   tmp.stx_nlink = stat->nlink;
+   tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
+   tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
+   tmp.stx_mode = stat->mode;
+   tmp.stx_ino = stat->ino;
+   tmp.stx_size = stat->size;
+   tmp.stx_blocks = stat->blocks;
+   tmp.stx_atime.tv_sec = stat->atime.tv_sec;
+   tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
+   tmp.stx_btime.tv_sec = stat->btime.tv_sec;
+   tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
+   tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
+   tmp.stx_ctime.tv_nsec =