Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!

Bug 42904

Summary: [ PATCH ] Variablehz patch in gentoo-sources kernels is incomplete, and causes erroneous tick count reports proportional to CONFIG_HZ
Product: Gentoo Linux Reporter: Rob Kovalchik <robk>
Component: [OLD] Core systemAssignee: x86-kernel (DEPRECATED) <x86-kernel>
Status: RESOLVED WONTFIX    
Severity: normal CC: x86-kernel
Priority: High    
Version: 1.4   
Hardware: All   
OS: Linux   
Whiteboard:
Package list:
Runtime testing required: ---

Description Rob Kovalchik 2004-02-25 09:53:33 UTC
The variablehz patch is designed to allow the kernel's tick resolution (HZ) to be increased from the vanilla default of 100Hz to some other value. For example, currently, the gentoo-sources kernel uses 1000Hz as the default.  

The patch also adds the notion of USER_HZ (hardcoded to 100) to be used as the tick units for all user mode interfaces regardless of the kernel's internal resolution.  Linus himself, as well as Alan Cox have stated on the LKML several times that regardless of the internal resolution, user mode processes should see 100 ticks per second resolution from all system calls that use time ticks as units.  Because of this requirement, the patch defines the jiffies_to_clock_t() macro, and applies it to several exported kernel routines to convert internal clock ticks to external clock ticks.  

The patch applied to the vanilla kernel to create the gentoo-sources kernel misses one of these kernel routines, namely sys_times() in kernel/sys.c.  Because of this error, the "times(struct tms t)" system call will report incorrect values if CONFIG_HZ in the running kernel is anything other than 100Hz.

Note: Other distributions that apply the variablehz patch (RedHat for one) also patch the sys_times() kernel function.

The specific kernel version I'm running is gentoo-sources-2.4.22-r5, but this bug is in many versions.

Reproducible: Always
Steps to Reproduce:
1. Compile and run a gentoo-sources kernel with CONFIG_HZ something other than 100 Hz.  It normally is set to 1000 Hz.
2. Compile and run this:

#include <unistd.h>
#include <sys/times.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    long i, t;
    struct tms ts;
    long tcps = sysconf(_SC_CLK_TCK);

    printf("Ticks per second is %ld.\n", tcps);

    for (i = 0; i < 5; ++i)
    {
            t = times(&ts);
            printf("Tick: %ld\n", t);
            sleep(1);
    }
    return 0;
}

Actual Results:  
Ticks per second is 100.
Tick: 5616605
Tick: 5617605
Tick: 5618605
Tick: 5619606
Tick: 5620606

Notice: While the sysconf call reports correctly, the times call is reporting
1000 ticks between each call instead of 100.  The error seen is proportional to
the kernel's compiled CONFIG_HZ / 100.  In this case, ten times off.


Expected Results:  
Something like this should have occurred:

Ticks per second is 100.
Tick: 5616605
Tick: 5616705
Tick: 5616805
Tick: 5616905
Tick: 5617006


Here's how I changed sys_times() in kernel/sys.c to correct the problem:

----------------------------------------
Original from gentoo-sources-2.4.22-r5
----------------------------------------
asmlinkage long sys_times(struct tms * tbuf)
{
        /*
         *      In the SMP world we might just be unlucky and have one of
         *      the times increment as we use it. Since the value is an
         *      atomically safe type this is just fine. Conceptually its
         *      as if the syscall took an instant longer to occur.
         */
        if (tbuf)
                if (copy_to_user(tbuf, &current->times, sizeof(struct tms)))
                        return -EFAULT;
        return jiffies;
}

----------------------------------------
My change
----------------------------------------
asmlinkage long sys_times(struct tms * tbuf)
{
        struct tms temp;
        /*
         *      In the SMP world we might just be unlucky and have one of
         *      the times increment as we use it. Since the value is an
         *      atomically safe type this is just fine. Conceptually its
         *      as if the syscall took an instant longer to occur.
         */
        if (tbuf)
        {
                temp.tms_utime = jiffies_to_clock_t(current->times.tms_utime);
                temp.tms_stime = jiffies_to_clock_t(current->times.tms_stime);
                temp.tms_cutime = jiffies_to_clock_t(current->times.tms_cutime);
                temp.tms_cstime = jiffies_to_clock_t(current->times.tms_cstime);

                if (copy_to_user(tbuf, &temp, sizeof(struct tms)))
                        return -EFAULT;
        }
        return jiffies_to_clock_t(jiffies);
}


--------------------------------------------------------
EMERGE INFO:
Portage 2.0.50-r1 (default-x86-1.4, gcc-3.2.3, glibc-2.3.2-r9, 2.4.22-gentoo-r5)
=================================================================
System uname: 2.4.22-gentoo-r5 i686 Pentium III (Coppermine)
Gentoo Base System version 1.4.3.10
Autoconf: sys-devel/autoconf-2.58
Automake: sys-devel/automake-1.7.7
ACCEPT_KEYWORDS="x86"
AUTOCLEAN="yes"
CFLAGS="-O3 -march=pentium3 -fprefetch-loop-arrays -funroll-loops -pipe"
CHOST="i686-pc-linux-gnu"
COMPILER="gcc3"
CONFIG_PROTECT="/etc /usr/X11R6/lib/X11/xkb /usr/kde/2/share/config
/usr/kde/3.1/share/config /usr/kde/3/share/config /usr/lib/mozilla/defaults/pref
/usr/share/config /var/qmail/control"
CONFIG_PROTECT_MASK="/etc/gconf /etc/env.d"
CXXFLAGS="-O3 -march=pentium3 -fprefetch-loop-arrays -funroll-loops -pipe"
DISTDIR="/usr/portage/distfiles"
FEATURES="autoaddcvs ccache sandbox"
GENTOO_MIRRORS="ftp://ftp.ussg.iu.edu/pub/linux/gentoo
http://cudlug.cudenver.edu/gentoo"
MAKEOPTS="-j2"
PKGDIR="/usr/portage/packages"
PORTAGE_TMPDIR="/var/tmp"
PORTDIR="/usr/portage"
PORTDIR_OVERLAY=""
SYNC="rsync://rsync.gentoo.org/gentoo-portage"
USE="X alsa apm arts avi berkdb crypt cups encode esd foomaticdb gdbm gif gtk2
imlib java jpeg kde libg++ libwww mad mikmod mmx motif mozcalendar mozilla mpeg
ncurses nls oggvorbis opengl oss pam pdflib perl png python qt quicktime
readline samba sdl slang spell ssl svga tcpd tiff truetype x86 xml2 xmms xv zlib"
Comment 1 Bob Johnson (RETIRED) gentoo-dev 2004-03-26 19:47:54 UTC
The gentoo patchset has been revamped, this patch is no longer included.