Author: cem
Date: Sun Feb 10 23:07:46 2019
New Revision: 343985
URL: https://svnweb.freebsd.org/changeset/base/343985

Log:
  Prevent overflow for usertime/systime in caclru1
  
  PR:           76972 and duplicates
  Reported by:  Dr. Christopher Landauer <cal AT aero.org>,
                Steinar Haug <sthaug AT nethelp.no>
  Submitted by: Andrey Zonov <andrey AT zonov.org> (earlier version)
  MFC after:    2 weeks

Modified:
  head/sys/kern/kern_resource.c

Modified: head/sys/kern/kern_resource.c
==============================================================================
--- head/sys/kern/kern_resource.c       Sun Feb 10 22:33:41 2019        
(r343984)
+++ head/sys/kern/kern_resource.c       Sun Feb 10 23:07:46 2019        
(r343985)
@@ -863,6 +863,15 @@ rufetchtd(struct thread *td, struct rusage *ru)
        calcru1(p, &td->td_rux, &ru->ru_utime, &ru->ru_stime);
 }
 
+static uint64_t
+mul64_by_fraction(uint64_t a, uint64_t b, uint64_t c)
+{
+       /*
+        * Compute floor(a * (b / c)) without overflowing, (b / c) <= 1.0.
+        */
+       return ((a / c) * b + (a % c) * (b / c) + (a % c) * (b % c) / c);
+}
+
 static void
 calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up,
     struct timeval *sp)
@@ -892,10 +901,10 @@ calcru1(struct proc *p, struct rusage_ext *ruxp, struc
                 * The normal case, time increased.
                 * Enforce monotonicity of bucketed numbers.
                 */
-               uu = (tu * ut) / tt;
+               uu = mul64_by_fraction(tu, ut, tt);
                if (uu < ruxp->rux_uu)
                        uu = ruxp->rux_uu;
-               su = (tu * st) / tt;
+               su = mul64_by_fraction(tu, st, tt);
                if (su < ruxp->rux_su)
                        su = ruxp->rux_su;
        } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) {
@@ -924,8 +933,8 @@ calcru1(struct proc *p, struct rusage_ext *ruxp, struc
                    "to %ju usec for pid %d (%s)\n",
                    (uintmax_t)ruxp->rux_tu, (uintmax_t)tu,
                    p->p_pid, p->p_comm);
-               uu = (tu * ut) / tt;
-               su = (tu * st) / tt;
+               uu = mul64_by_fraction(tu, ut, tt);
+               su = mul64_by_fraction(tu, st, tt);
        }
 
        ruxp->rux_uu = uu;
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to