Skip to content

Commit

Permalink
[PATCH] Common compat_sys_sysinfo
Browse files Browse the repository at this point in the history
I noticed that almost all architectures implemented exactly the same
sys32_sysinfo...  except parisc, where a bug was to be found in handling of
the uptime.  So let's remove a whole whack of code for fun and profit.
Cribbed compat_sys_sysinfo from x86_64's implementation, since I figured it
would be the best tested.

This patch incorporates Arnd's suggestion of not using set_fs/get_fs, but
instead extracting out the common code from sys_sysinfo.

Cc: Christoph Hellwig <[email protected]>
Cc: <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Kyle McMartin authored and Linus Torvalds committed Feb 11, 2007
1 parent 72fd4a3 commit d4d23ad
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 453 deletions.
2 changes: 1 addition & 1 deletion arch/ia64/ia32/ia32_entry.S
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ ia32_syscall_table:
data8 sys_ni_syscall
data8 compat_sys_wait4
data8 sys_swapoff /* 115 */
data8 sys32_sysinfo
data8 compat_sys_sysinfo
data8 sys32_ipc
data8 sys_fsync
data8 sys32_sigreturn
Expand Down
68 changes: 0 additions & 68 deletions arch/ia64/ia32/sys_ia32.c
Original file line number Diff line number Diff line change
Expand Up @@ -2209,74 +2209,6 @@ sys32_fstat64 (unsigned int fd, struct stat64 __user *statbuf)
return ret;
}

struct sysinfo32 {
s32 uptime;
u32 loads[3];
u32 totalram;
u32 freeram;
u32 sharedram;
u32 bufferram;
u32 totalswap;
u32 freeswap;
u16 procs;
u16 pad;
u32 totalhigh;
u32 freehigh;
u32 mem_unit;
char _f[8];
};

asmlinkage long
sys32_sysinfo (struct sysinfo32 __user *info)
{
struct sysinfo s;
long ret, err;
int bitcount = 0;
mm_segment_t old_fs = get_fs();

set_fs(KERNEL_DS);
ret = sys_sysinfo((struct sysinfo __user *) &s);
set_fs(old_fs);
/* Check to see if any memory value is too large for 32-bit and
* scale down if needed.
*/
if ((s.totalram >> 32) || (s.totalswap >> 32)) {
while (s.mem_unit < PAGE_SIZE) {
s.mem_unit <<= 1;
bitcount++;
}
s.totalram >>= bitcount;
s.freeram >>= bitcount;
s.sharedram >>= bitcount;
s.bufferram >>= bitcount;
s.totalswap >>= bitcount;
s.freeswap >>= bitcount;
s.totalhigh >>= bitcount;
s.freehigh >>= bitcount;
}

if (!access_ok(VERIFY_WRITE, info, sizeof(*info)))
return -EFAULT;

err = __put_user(s.uptime, &info->uptime);
err |= __put_user(s.loads[0], &info->loads[0]);
err |= __put_user(s.loads[1], &info->loads[1]);
err |= __put_user(s.loads[2], &info->loads[2]);
err |= __put_user(s.totalram, &info->totalram);
err |= __put_user(s.freeram, &info->freeram);
err |= __put_user(s.sharedram, &info->sharedram);
err |= __put_user(s.bufferram, &info->bufferram);
err |= __put_user(s.totalswap, &info->totalswap);
err |= __put_user(s.freeswap, &info->freeswap);
err |= __put_user(s.procs, &info->procs);
err |= __put_user (s.totalhigh, &info->totalhigh);
err |= __put_user (s.freehigh, &info->freehigh);
err |= __put_user (s.mem_unit, &info->mem_unit);
if (err)
return -EFAULT;
return ret;
}

asmlinkage long
sys32_sched_rr_get_interval (pid_t pid, struct compat_timespec __user *interval)
{
Expand Down
44 changes: 0 additions & 44 deletions arch/mips/kernel/linux32.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,50 +193,6 @@ sysn32_waitid(int which, compat_pid_t pid,
return ret;
}

struct sysinfo32 {
s32 uptime;
u32 loads[3];
u32 totalram;
u32 freeram;
u32 sharedram;
u32 bufferram;
u32 totalswap;
u32 freeswap;
u16 procs;
u32 totalhigh;
u32 freehigh;
u32 mem_unit;
char _f[8];
};

asmlinkage int sys32_sysinfo(struct sysinfo32 __user *info)
{
struct sysinfo s;
int ret, err;
mm_segment_t old_fs = get_fs ();

set_fs (KERNEL_DS);
ret = sys_sysinfo((struct sysinfo __user *)&s);
set_fs (old_fs);
err = put_user (s.uptime, &info->uptime);
err |= __put_user (s.loads[0], &info->loads[0]);
err |= __put_user (s.loads[1], &info->loads[1]);
err |= __put_user (s.loads[2], &info->loads[2]);
err |= __put_user (s.totalram, &info->totalram);
err |= __put_user (s.freeram, &info->freeram);
err |= __put_user (s.sharedram, &info->sharedram);
err |= __put_user (s.bufferram, &info->bufferram);
err |= __put_user (s.totalswap, &info->totalswap);
err |= __put_user (s.freeswap, &info->freeswap);
err |= __put_user (s.procs, &info->procs);
err |= __put_user (s.totalhigh, &info->totalhigh);
err |= __put_user (s.freehigh, &info->freehigh);
err |= __put_user (s.mem_unit, &info->mem_unit);
if (err)
return -EFAULT;
return ret;
}

#define RLIM_INFINITY32 0x7fffffff
#define RESOURCE32(x) ((x > RLIM_INFINITY32) ? RLIM_INFINITY32 : x)

Expand Down
2 changes: 1 addition & 1 deletion arch/mips/kernel/scall64-n32.S
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ EXPORT(sysn32_call_table)
PTR sys32_gettimeofday
PTR compat_sys_getrlimit /* 6095 */
PTR compat_sys_getrusage
PTR sys32_sysinfo
PTR compat_sys_sysinfo
PTR compat_sys_times
PTR sys32_ptrace
PTR sys_getuid /* 6100 */
Expand Down
2 changes: 1 addition & 1 deletion arch/mips/kernel/scall64-o32.S
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ sys_call_table:
PTR sys_ni_syscall /* sys_vm86 */
PTR compat_sys_wait4
PTR sys_swapoff /* 4115 */
PTR sys32_sysinfo
PTR compat_sys_sysinfo
PTR sys32_ipc
PTR sys_fsync
PTR sys32_sigreturn
Expand Down
64 changes: 0 additions & 64 deletions arch/parisc/kernel/sys_parisc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,70 +579,6 @@ asmlinkage int sys32_sendfile64(int out_fd, int in_fd, compat_loff_t __user *off
}


struct sysinfo32 {
s32 uptime;
u32 loads[3];
u32 totalram;
u32 freeram;
u32 sharedram;
u32 bufferram;
u32 totalswap;
u32 freeswap;
unsigned short procs;
u32 totalhigh;
u32 freehigh;
u32 mem_unit;
char _f[12];
};

/* We used to call sys_sysinfo and translate the result. But sys_sysinfo
* undoes the good work done elsewhere, and rather than undoing the
* damage, I decided to just duplicate the code from sys_sysinfo here.
*/

asmlinkage int sys32_sysinfo(struct sysinfo32 __user *info)
{
struct sysinfo val;
int err;
unsigned long seq;

/* We don't need a memset here because we copy the
* struct to userspace once element at a time.
*/

do {
seq = read_seqbegin(&xtime_lock);
val.uptime = jiffies / HZ;

val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);

val.procs = nr_threads;
} while (read_seqretry(&xtime_lock, seq));


si_meminfo(&val);
si_swapinfo(&val);

err = put_user (val.uptime, &info->uptime);
err |= __put_user (val.loads[0], &info->loads[0]);
err |= __put_user (val.loads[1], &info->loads[1]);
err |= __put_user (val.loads[2], &info->loads[2]);
err |= __put_user (val.totalram, &info->totalram);
err |= __put_user (val.freeram, &info->freeram);
err |= __put_user (val.sharedram, &info->sharedram);
err |= __put_user (val.bufferram, &info->bufferram);
err |= __put_user (val.totalswap, &info->totalswap);
err |= __put_user (val.freeswap, &info->freeswap);
err |= __put_user (val.procs, &info->procs);
err |= __put_user (val.totalhigh, &info->totalhigh);
err |= __put_user (val.freehigh, &info->freehigh);
err |= __put_user (val.mem_unit, &info->mem_unit);
return err ? -EFAULT : 0;
}


/* lseek() needs a wrapper because 'offset' can be negative, but the top
* half of the argument has been zeroed by syscall.S.
*/
Expand Down
2 changes: 1 addition & 1 deletion arch/parisc/kernel/syscall_table.S
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
/* struct rusage contains longs... */
ENTRY_COMP(wait4)
ENTRY_SAME(swapoff) /* 115 */
ENTRY_DIFF(sysinfo)
ENTRY_COMP(sysinfo)
ENTRY_SAME(shutdown)
ENTRY_SAME(fsync)
ENTRY_SAME(madvise)
Expand Down
67 changes: 0 additions & 67 deletions arch/powerpc/kernel/sys_ppc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,73 +198,6 @@ static inline long put_tv32(struct compat_timeval __user *o, struct timeval *i)
__put_user(i->tv_usec, &o->tv_usec)));
}

struct sysinfo32 {
s32 uptime;
u32 loads[3];
u32 totalram;
u32 freeram;
u32 sharedram;
u32 bufferram;
u32 totalswap;
u32 freeswap;
unsigned short procs;
unsigned short pad;
u32 totalhigh;
u32 freehigh;
u32 mem_unit;
char _f[20-2*sizeof(int)-sizeof(int)];
};

asmlinkage long compat_sys_sysinfo(struct sysinfo32 __user *info)
{
struct sysinfo s;
int ret, err;
int bitcount=0;
mm_segment_t old_fs = get_fs ();

/* The __user cast is valid due to set_fs() */
set_fs (KERNEL_DS);
ret = sys_sysinfo((struct sysinfo __user *)&s);
set_fs (old_fs);

/* Check to see if any memory value is too large for 32-bit and
* scale down if needed.
*/
if ((s.totalram >> 32) || (s.totalswap >> 32)) {
while (s.mem_unit < PAGE_SIZE) {
s.mem_unit <<= 1;
bitcount++;
}
s.totalram >>=bitcount;
s.freeram >>= bitcount;
s.sharedram >>= bitcount;
s.bufferram >>= bitcount;
s.totalswap >>= bitcount;
s.freeswap >>= bitcount;
s.totalhigh >>= bitcount;
s.freehigh >>= bitcount;
}

err = put_user (s.uptime, &info->uptime);
err |= __put_user (s.loads[0], &info->loads[0]);
err |= __put_user (s.loads[1], &info->loads[1]);
err |= __put_user (s.loads[2], &info->loads[2]);
err |= __put_user (s.totalram, &info->totalram);
err |= __put_user (s.freeram, &info->freeram);
err |= __put_user (s.sharedram, &info->sharedram);
err |= __put_user (s.bufferram, &info->bufferram);
err |= __put_user (s.totalswap, &info->totalswap);
err |= __put_user (s.freeswap, &info->freeswap);
err |= __put_user (s.procs, &info->procs);
err |= __put_user (s.totalhigh, &info->totalhigh);
err |= __put_user (s.freehigh, &info->freehigh);
err |= __put_user (s.mem_unit, &info->mem_unit);
if (err)
return -EFAULT;

return ret;
}




Expand Down
45 changes: 0 additions & 45 deletions arch/s390/kernel/compat_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,51 +398,6 @@ int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
return err;
}

struct sysinfo32 {
s32 uptime;
u32 loads[3];
u32 totalram;
u32 freeram;
u32 sharedram;
u32 bufferram;
u32 totalswap;
u32 freeswap;
unsigned short procs;
unsigned short pads;
u32 totalhigh;
u32 freehigh;
unsigned int mem_unit;
char _f[8];
};

asmlinkage long sys32_sysinfo(struct sysinfo32 __user *info)
{
struct sysinfo s;
int ret, err;
mm_segment_t old_fs = get_fs ();

set_fs (KERNEL_DS);
ret = sys_sysinfo((struct sysinfo __force __user *) &s);
set_fs (old_fs);
err = put_user (s.uptime, &info->uptime);
err |= __put_user (s.loads[0], &info->loads[0]);
err |= __put_user (s.loads[1], &info->loads[1]);
err |= __put_user (s.loads[2], &info->loads[2]);
err |= __put_user (s.totalram, &info->totalram);
err |= __put_user (s.freeram, &info->freeram);
err |= __put_user (s.sharedram, &info->sharedram);
err |= __put_user (s.bufferram, &info->bufferram);
err |= __put_user (s.totalswap, &info->totalswap);
err |= __put_user (s.freeswap, &info->freeswap);
err |= __put_user (s.procs, &info->procs);
err |= __put_user (s.totalhigh, &info->totalhigh);
err |= __put_user (s.freehigh, &info->freehigh);
err |= __put_user (s.mem_unit, &info->mem_unit);
if (err)
return -EFAULT;
return ret;
}

asmlinkage long sys32_sched_rr_get_interval(compat_pid_t pid,
struct compat_timespec __user *interval)
{
Expand Down
6 changes: 3 additions & 3 deletions arch/s390/kernel/compat_wrapper.S
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,10 @@ sys32_swapoff_wrapper:
llgtr %r2,%r2 # const char *
jg sys_swapoff # branch to system call

.globl sys32_sysinfo_wrapper
sys32_sysinfo_wrapper:
.globl compat_sys_sysinfo_wrapper
compat_sys_sysinfo_wrapper:
llgtr %r2,%r2 # struct sysinfo_emu31 *
jg sys32_sysinfo # branch to system call
jg compat_sys_sysinfo # branch to system call

.globl sys32_ipc_wrapper
sys32_ipc_wrapper:
Expand Down
2 changes: 1 addition & 1 deletion arch/s390/kernel/syscalls.S
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ NI_SYSCALL /* old "idle" system call */
NI_SYSCALL /* vm86old for i386 */
SYSCALL(sys_wait4,sys_wait4,compat_sys_wait4_wrapper)
SYSCALL(sys_swapoff,sys_swapoff,sys32_swapoff_wrapper) /* 115 */
SYSCALL(sys_sysinfo,sys_sysinfo,sys32_sysinfo_wrapper)
SYSCALL(sys_sysinfo,sys_sysinfo,compat_sys_sysinfo_wrapper)
SYSCALL(sys_ipc,sys_ipc,sys32_ipc_wrapper)
SYSCALL(sys_fsync,sys_fsync,sys32_fsync_wrapper)
SYSCALL(sys_sigreturn_glue,sys_sigreturn_glue,sys32_sigreturn_glue)
Expand Down
Loading

0 comments on commit d4d23ad

Please sign in to comment.