linux-user/syscall.c: consolidate statfs syscalls
cover linux-user/syscall.c: consolidate statfs syscalls
Here's some cleanup for linux-user/syscall.c with an RFC.
It drops common ("global") struct statfs from syscall1() function and eliminates an ugly goto into another case label.
The RFC is for the 3rd patch, which replaces two blocks of the smae code for statfs & statfs64 (which only differ in the 64 suffix) with a common macro. This way there's a single implementation of the code, so we avoid code duplication, but the usage of the C preprocessor this way makes the code a bit more ugly.
What do you think about this approach? Using it, a lot of code in there can be eliminated.
Michael Tokarev (3):
linux-user/syscall.c: statfs: f_flags is always present linux-user/syscall.c: consolidate statfs calls linux-user/syscall.c: consolidate statfs calls further
linux-user/syscall.c | 114 +++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 75 deletions(-)
Signature · 1 line
2.47.3
PATCH 1/3 linux-user/syscall.c: statfs: f_flags is always present
statfs.f_flags is present since linux 2.6. There's no need to check for its existance anymore.
--- linux-user/syscall.c | 8 -------- 1 file changed, 8 deletions(-)
linux-user/syscall.c +0 -8
@@ -11000,11 +11000,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,__put_user (stfs.f_fsid.__val[1 ], &target_stfs->f_fsid.val[1 ]);__put_user (stfs.f_namelen, &target_stfs->f_namelen);__put_user (stfs.f_frsize, &target_stfs->f_frsize); -#ifdef _STATFS_F_FLAGS__put_user (stfs.f_flags, &target_stfs->f_flags); -#else -__put_user (0 , &target_stfs->f_flags); -#endif memset (target_stfs->f_spare,0 ,sizeof (target_stfs->f_spare));unlock_user_struct (target_stfs, arg2,1 ); }
@@ -11039,11 +11035,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,__put_user (stfs.f_fsid.__val[1 ], &target_stfs->f_fsid.val[1 ]);__put_user (stfs.f_namelen, &target_stfs->f_namelen);__put_user (stfs.f_frsize, &target_stfs->f_frsize); -#ifdef _STATFS_F_FLAGS__put_user (stfs.f_flags, &target_stfs->f_flags); -#else -__put_user (0 , &target_stfs->f_flags); -#endif memset (target_stfs->f_spare,0 ,sizeof (target_stfs->f_spare));unlock_user_struct (target_stfs, arg3,1 ); }
Signature · 1 line
2.47.3
PATCH 2/3 linux-user/syscall.c: consolidate statfs calls
In reply to Michael Tokarevgroup statfs&fstatfs together, eliminate goto into a different case label, eliminate struct statfs global to all syscalls. Ditto for statfs64.
This makes code more readable and less scattered.
While at it, drop #ifdef TARGET_NR_fstatfs -- assume fstatfs() is present together with statfs() - just like for fstatfs64.
--- linux-user/syscall.c | 62 +++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 30 deletions(-)
linux-user/syscall.c +32 -30
@@ -9457,10 +9457,6 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \ || defined(TARGET_NR_statx)struct stat st; -#endif -#if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \ - || defined(TARGET_NR_fstatfs) -struct statfs stfs;#endif void *p;
@@ -10978,15 +10974,22 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,return get_errno (setpriority (arg1, arg2, arg3));#ifdef TARGET_NR_statfscase TARGET_NR_statfs: -if (!(p =lock_user_string (arg1))) { -return -TARGET_EFAULT ; - } - ret =get_errno (statfs (path (p), &stfs)); -unlock_user (p, arg1,0 ); - convert_statfs: -if (!is_error (ret)) { +case TARGET_NR_fstatfs: + { +struct statf stfs;struct target_statfs *target_stfs; - +if (num == TARGET_NR_statfs) { +if (!(p =lock_user_string (arg1))) { +return -TARGET_EFAULT ; + } + ret =get_errno (statfs (path (p), &stfs)); +unlock_user (p, arg1,0 ); + }else /* if (num == TARGET_NR_fstatfs) */ { + ret =get_errno (fstatfs (arg1, &stfs)); + } +if (is_error (ret)) { +return ret; + }if (!lock_user_struct (VERIFY_WRITE , target_stfs, arg2,0 ))return -TARGET_EFAULT ;__put_user (stfs.f_type, &target_stfs->f_type);
@@ -11003,25 +11006,27 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,__put_user (stfs.f_flags, &target_stfs->f_flags);memset (target_stfs->f_spare,0 ,sizeof (target_stfs->f_spare));unlock_user_struct (target_stfs, arg2,1 ); +return ret; } -return ret; -#endif -#ifdef TARGET_NR_fstatfs -case TARGET_NR_fstatfs: - ret =get_errno (fstatfs (arg1, &stfs)); - goto convert_statfs;#endif #ifdef TARGET_NR_statfs64case TARGET_NR_statfs64: -if (!(p =lock_user_string (arg1))) { -return -TARGET_EFAULT ; - } - ret =get_errno (statfs (path (p), &stfs)); -unlock_user (p, arg1,0 ); - convert_statfs64: -if (!is_error (ret)) { +case TARGET_NR_fstatfs64: + { +struct statfs stfs;struct target_statfs64 *target_stfs; - +if (num == TARGET_NR_statfs64) { +if (!(p =lock_user_string (arg1))) { +return -TARGET_EFAULT ; + } + ret =get_errno (statfs (path (p), &stfs)); +unlock_user (p, arg1,0 ); + }else /* if (num == TARGET_NR_fstatfs64) */ { + ret =get_errno (fstatfs (arg1, &stfs)); + } +if (is_error (ret)) { +return ret; + }if (!lock_user_struct (VERIFY_WRITE , target_stfs, arg3,0 ))return -TARGET_EFAULT ;__put_user (stfs.f_type, &target_stfs->f_type);
@@ -11038,11 +11043,8 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,__put_user (stfs.f_flags, &target_stfs->f_flags);memset (target_stfs->f_spare,0 ,sizeof (target_stfs->f_spare));unlock_user_struct (target_stfs, arg3,1 ); + return ret; } -return ret; -case TARGET_NR_fstatfs64: - ret =get_errno (fstatfs (arg1, &stfs)); - goto convert_statfs64;#endif #ifdef TARGET_NR_socketcallcase TARGET_NR_socketcall:
Signature · 1 line
2.47.3
Re: [PATCH 2/3] linux-user/syscall.c: consolidate statfs calls
Quoted 8 lines · Michael Tokarev
group statfs&fstatfs together, eliminate goto into a different case label, eliminate struct statfs global to all syscalls. Ditto for statfs64.
This makes code more readable and less scattered.
While at it, drop
#ifdef TARGET_NR_fstatfs-- assumefstatfs()is present together withstatfs()- just like for fstatfs64.
Even better would be to split out helper functions; do_syscall1 and its switch statement is simply too big.
r~
RFC PATCH 3/3 linux-user/syscall.c: consolidate statfs calls further
In reply to Michael TokarevSince statfs & statfs64 implementations are exactly the same, differs only in "64" suffix, merge them into one using a common macro.
--- linux-user/syscall.c | 110 ++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 70 deletions(-)
linux-user/syscall.c +40 -70
@@ -10972,80 +10972,50 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,return ret;case TARGET_NR_setpriority:return get_errno (setpriority (arg1, arg2, arg3)); -#ifdef TARGET_NR_statfs -case TARGET_NR_statfs: -case TARGET_NR_fstatfs: - { -struct statf stfs; -struct target_statfs *target_stfs; -if (num == TARGET_NR_statfs) { -if (!(p =lock_user_string (arg1))) { -return -TARGET_EFAULT ; - } - ret =get_errno (statfs (path (p), &stfs)); -unlock_user (p, arg1,0 ); - }else /* if (num == TARGET_NR_fstatfs) */ { - ret =get_errno (fstatfs (arg1, &stfs)); - } -if (is_error (ret)) { -return ret; - } -if (!lock_user_struct (VERIFY_WRITE , target_stfs, arg2,0 )) -return -TARGET_EFAULT ; -__put_user (stfs.f_type, &target_stfs->f_type); -__put_user (stfs.f_bsize, &target_stfs->f_bsize); -__put_user (stfs.f_blocks, &target_stfs->f_blocks); -__put_user (stfs.f_bfree, &target_stfs->f_bfree); -__put_user (stfs.f_bavail, &target_stfs->f_bavail); -__put_user (stfs.f_files, &target_stfs->f_files); -__put_user (stfs.f_ffree, &target_stfs->f_ffree); -__put_user (stfs.f_fsid.__val[0 ], &target_stfs->f_fsid.val[0 ]); -__put_user (stfs.f_fsid.__val[1 ], &target_stfs->f_fsid.val[1 ]); -__put_user (stfs.f_namelen, &target_stfs->f_namelen); -__put_user (stfs.f_frsize, &target_stfs->f_frsize); -__put_user (stfs.f_flags, &target_stfs->f_flags); -memset (target_stfs->f_spare,0 ,sizeof (target_stfs->f_spare)); -unlock_user_struct (target_stfs, arg2,1 ); -return ret; + +#define statfs_fstatfs_impl (variant)/* variant is statfs or statfs64 */ \ + case TARGET_NR_##variant: \ + case TARGET_NR_f##variant: \ + { \ + struct statfs stfs; \ + struct target_##variant *target_stfs; \ + if (num == TARGET_NR_##variant) { \ + if (!(p = lock_user_string(arg1))) { \ + return -TARGET_EFAULT; \ + } \ + ret = get_errno(statfs(path(p), &stfs)); \ + unlock_user(p, arg1, 0); \ + } else/* if (num == TARGET_NR_f##variant) */ { \ + ret =get_errno (fstatfs (arg1, &stfs)); \ + } \ +if (is_error (ret)) { \ +return ret; \ + } \ +if (!lock_user_struct (VERIFY_WRITE , target_stfs, arg2,0 )) \ +return -TARGET_EFAULT ; \ +__put_user (stfs.f_type, &target_stfs->f_type); \ +__put_user (stfs.f_bsize, &target_stfs->f_bsize); \ +__put_user (stfs.f_blocks, &target_stfs->f_blocks); \ +__put_user (stfs.f_bfree, &target_stfs->f_bfree); \ +__put_user (stfs.f_bavail, &target_stfs->f_bavail); \ +__put_user (stfs.f_files, &target_stfs->f_files); \ +__put_user (stfs.f_ffree, &target_stfs->f_ffree); \ +__put_user (stfs.f_fsid.__val[0 ], &target_stfs->f_fsid.val[0 ]); \ +__put_user (stfs.f_fsid.__val[1 ], &target_stfs->f_fsid.val[1 ]); \ +__put_user (stfs.f_namelen, &target_stfs->f_namelen); \ +__put_user (stfs.f_frsize, &target_stfs->f_frsize); \ +__put_user (stfs.f_flags, &target_stfs->f_flags); \ +memset (target_stfs->f_spare,0 ,sizeof (target_stfs->f_spare)); \ +unlock_user_struct (target_stfs, arg2,1 ); \ +return ret; \ } +#ifdef TARGET_NR_statfs +statfs_fstatfs_impl (statfs);#endif #ifdef TARGET_NR_statfs64 -case TARGET_NR_statfs64: -case TARGET_NR_fstatfs64: - { -struct statfs stfs; -struct target_statfs64 *target_stfs; -if (num == TARGET_NR_statfs64) { -if (!(p =lock_user_string (arg1))) { -return -TARGET_EFAULT ; - } - ret =get_errno (statfs (path (p), &stfs)); -unlock_user (p, arg1,0 ); - }else /* if (num == TARGET_NR_fstatfs64) */ { - ret =get_errno (fstatfs (arg1, &stfs)); - } -if (is_error (ret)) { -return ret; - } -if (!lock_user_struct (VERIFY_WRITE , target_stfs, arg3,0 )) -return -TARGET_EFAULT ; -__put_user (stfs.f_type, &target_stfs->f_type); -__put_user (stfs.f_bsize, &target_stfs->f_bsize); -__put_user (stfs.f_blocks, &target_stfs->f_blocks); -__put_user (stfs.f_bfree, &target_stfs->f_bfree); -__put_user (stfs.f_bavail, &target_stfs->f_bavail); -__put_user (stfs.f_files, &target_stfs->f_files); -__put_user (stfs.f_ffree, &target_stfs->f_ffree); -__put_user (stfs.f_fsid.__val[0 ], &target_stfs->f_fsid.val[0 ]); -__put_user (stfs.f_fsid.__val[1 ], &target_stfs->f_fsid.val[1 ]); -__put_user (stfs.f_namelen, &target_stfs->f_namelen); -__put_user (stfs.f_frsize, &target_stfs->f_frsize); -__put_user (stfs.f_flags, &target_stfs->f_flags); -memset (target_stfs->f_spare,0 ,sizeof (target_stfs->f_spare)); -unlock_user_struct (target_stfs, arg3,1 ); -return ret; - } +statfs_fstatfs_impl (statfs64);#endif +#ifdef TARGET_NR_socketcallcase TARGET_NR_socketcall:return do_socketcall (arg1, arg2);
Signature · 1 line
2.47.3
Re: [PATCH RFC 3/3] linux-user/syscall.c: consolidate statfs calls further
Quoted 5 lines · Michael Tokarev
Since statfs & statfs64 implementations are exactly the same, differs only in "64" suffix, merge them into one using a common macro.
Signed-off-by Michael Tokarev <[email protected]>
Most of the common code should be split to a host_to_target_statfs() and(?) host_to_target_statfs64(). Once that's done, there's not much use in a macro.
r~
Re: [PATCH RFC 3/3] linux-user/syscall.c: consolidate statfs calls further
Quoted 8 lines · Richard Henderson
Quoted 5 lines · Michael Tokarev
Since statfs & statfs64 implementations are exactly the same, differs only in "64" suffix, merge them into one using a common macro.
Signed-off-by Michael Tokarev <[email protected]>Most of the common code should be split to a
host_to_target_statfs()and(?) host_to_target_statfs64(). Once that's done, there's not much use in a macro.
It will be exactly the same, - be it in its own function(s) or directly in syscall1(): there will be 2 copies of exactly the same text, dealing with 2 different types of target_statfs structs. This is where a macro like in this patch/rfc comes into play. An alternative is C++ template mechanism (which doesn't work in C, obviously), or a repeat of the same code - just like we have now.
The same situation is with struct stat, but it is much worse, because there are more possible permutations - there's eabi_stat64 on ARM and struct target_stat vs target_stat64, and several combinations of TARGET_STAT64_HAS_BROKEN_ST_INO and HAVE_STRUCT_STAT_ST_ATIM. It's kind of insane in this context. While it's trivial to use a macro approach for statfs, it's not as easy for struct stat.
Personally, I see no big advantage of splitting some syscalls into separate functions out of huge syscall1(). Yes, this function is large, but it wont be much better if some parts of it are split out, but there will be slightly more code (function definitions). Maybe a bit more.. style, so to say, but splitting it wont give us much..
Thanks,
/mjt