From owner-svn-src-stable@freebsd.org Fri Mar 6 13:57:01 2020 Return-Path: Delivered-To: svn-src-stable@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id DF1B9269209; Fri, 6 Mar 2020 13:57:01 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 48Yq190f87z4B4D; Fri, 6 Mar 2020 13:57:01 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id A35A32117C; Fri, 6 Mar 2020 13:57:00 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 026Dv0BW091770; Fri, 6 Mar 2020 13:57:00 GMT (envelope-from hselasky@FreeBSD.org) Received: (from hselasky@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 026Dv03v091769; Fri, 6 Mar 2020 13:57:00 GMT (envelope-from hselasky@FreeBSD.org) Message-Id: <202003061357.026Dv03v091769@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: hselasky set sender to hselasky@FreeBSD.org using -f From: Hans Petter Selasky Date: Fri, 6 Mar 2020 13:57:00 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r358699 - in stable/12/sys/compat/linuxkpi/common: include/linux src X-SVN-Group: stable-12 X-SVN-Commit-Author: hselasky X-SVN-Commit-Paths: in stable/12/sys/compat/linuxkpi/common: include/linux src X-SVN-Commit-Revision: 358699 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 Mar 2020 13:57:02 -0000 Author: hselasky Date: Fri Mar 6 13:57:00 2020 New Revision: 358699 URL: https://svnweb.freebsd.org/changeset/base/358699 Log: MFC r358387: Extend the range of the return value from nsecs_to_jiffies64() to support Mesa's drm_syncobj usage, in the LinuxKPI. While at it optimise the jiffies conversion functions to avoid repeated and constant calculations. Submitted by: Greg V Differential Revision: https://reviews.freebsd.org/D23846 Sponsored by: Mellanox Technologies Modified: stable/12/sys/compat/linuxkpi/common/include/linux/jiffies.h stable/12/sys/compat/linuxkpi/common/src/linux_compat.c Directory Properties: stable/12/ (props changed) Modified: stable/12/sys/compat/linuxkpi/common/include/linux/jiffies.h ============================================================================== --- stable/12/sys/compat/linuxkpi/common/include/linux/jiffies.h Fri Mar 6 12:37:04 2020 (r358698) +++ stable/12/sys/compat/linuxkpi/common/include/linux/jiffies.h Fri Mar 6 13:57:00 2020 (r358699) @@ -54,6 +54,18 @@ #define HZ hz +extern uint64_t lkpi_nsec2hz_rem; +extern uint64_t lkpi_nsec2hz_div; +extern uint64_t lkpi_nsec2hz_max; + +extern uint64_t lkpi_usec2hz_rem; +extern uint64_t lkpi_usec2hz_div; +extern uint64_t lkpi_usec2hz_max; + +extern uint64_t lkpi_msec2hz_rem; +extern uint64_t lkpi_msec2hz_div; +extern uint64_t lkpi_msec2hz_max; + static inline int timespec_to_jiffies(const struct timespec *ts) { @@ -70,12 +82,11 @@ timespec_to_jiffies(const struct timespec *ts) static inline int msecs_to_jiffies(uint64_t msec) { - uint64_t msec_max, result; + uint64_t result; - msec_max = -1ULL / (uint64_t)hz; - if (msec > msec_max) - msec = msec_max; - result = howmany(msec * (uint64_t)hz, 1000ULL); + if (msec > lkpi_msec2hz_max) + msec = lkpi_msec2hz_max; + result = howmany(msec * lkpi_msec2hz_rem, lkpi_msec2hz_div); if (result > MAX_JIFFY_OFFSET) result = MAX_JIFFY_OFFSET; @@ -85,12 +96,11 @@ msecs_to_jiffies(uint64_t msec) static inline int usecs_to_jiffies(uint64_t usec) { - uint64_t usec_max, result; + uint64_t result; - usec_max = -1ULL / (uint64_t)hz; - if (usec > usec_max) - usec = usec_max; - result = howmany(usec * (uint64_t)hz, 1000000ULL); + if (usec > lkpi_usec2hz_max) + usec = lkpi_usec2hz_max; + result = howmany(usec * lkpi_usec2hz_rem, lkpi_usec2hz_div); if (result > MAX_JIFFY_OFFSET) result = MAX_JIFFY_OFFSET; @@ -100,23 +110,24 @@ usecs_to_jiffies(uint64_t usec) static inline uint64_t nsecs_to_jiffies64(uint64_t nsec) { - uint64_t nsec_max, result; - nsec_max = -1ULL / (uint64_t)hz; - if (nsec > nsec_max) - nsec = nsec_max; - result = howmany(nsec * (uint64_t)hz, 1000000000ULL); - if (result > MAX_JIFFY_OFFSET) - result = MAX_JIFFY_OFFSET; - - return (result); + if (nsec > lkpi_nsec2hz_max) + nsec = lkpi_nsec2hz_max; + return (howmany(nsec * lkpi_nsec2hz_rem, lkpi_nsec2hz_div)); } -static inline uint64_t -nsecs_to_jiffies(uint64_t n) +static inline unsigned long +nsecs_to_jiffies(uint64_t nsec) { - return (usecs_to_jiffies(howmany(n, 1000ULL))); + if (sizeof(unsigned long) >= sizeof(uint64_t)) { + if (nsec > lkpi_nsec2hz_max) + nsec = lkpi_nsec2hz_max; + } else { + if (nsec > (lkpi_nsec2hz_max >> 32)) + nsec = (lkpi_nsec2hz_max >> 32); + } + return (howmany(nsec * lkpi_nsec2hz_rem, lkpi_nsec2hz_div)); } static inline uint64_t Modified: stable/12/sys/compat/linuxkpi/common/src/linux_compat.c ============================================================================== --- stable/12/sys/compat/linuxkpi/common/src/linux_compat.c Fri Mar 6 12:37:04 2020 (r358698) +++ stable/12/sys/compat/linuxkpi/common/src/linux_compat.c Fri Mar 6 13:57:00 2020 (r358699) @@ -1934,9 +1934,38 @@ del_timer(struct timer_list *timer) return (1); } +/* greatest common divisor, Euclid equation */ +static uint64_t +lkpi_gcd_64(uint64_t a, uint64_t b) +{ + uint64_t an; + uint64_t bn; + + while (b != 0) { + an = b; + bn = a % b; + a = an; + b = bn; + } + return (a); +} + +uint64_t lkpi_nsec2hz_rem; +uint64_t lkpi_nsec2hz_div = 1000000000ULL; +uint64_t lkpi_nsec2hz_max; + +uint64_t lkpi_usec2hz_rem; +uint64_t lkpi_usec2hz_div = 1000000ULL; +uint64_t lkpi_usec2hz_max; + +uint64_t lkpi_msec2hz_rem; +uint64_t lkpi_msec2hz_div = 1000ULL; +uint64_t lkpi_msec2hz_max; + static void linux_timer_init(void *arg) { + uint64_t gcd; /* * Compute an internal HZ value which can divide 2**32 to @@ -1947,6 +1976,27 @@ linux_timer_init(void *arg) while (linux_timer_hz_mask < (unsigned long)hz) linux_timer_hz_mask *= 2; linux_timer_hz_mask--; + + /* compute some internal constants */ + + lkpi_nsec2hz_rem = hz; + lkpi_usec2hz_rem = hz; + lkpi_msec2hz_rem = hz; + + gcd = lkpi_gcd_64(lkpi_nsec2hz_rem, lkpi_nsec2hz_div); + lkpi_nsec2hz_rem /= gcd; + lkpi_nsec2hz_div /= gcd; + lkpi_nsec2hz_max = -1ULL / lkpi_nsec2hz_rem; + + gcd = lkpi_gcd_64(lkpi_usec2hz_rem, lkpi_usec2hz_div); + lkpi_usec2hz_rem /= gcd; + lkpi_usec2hz_div /= gcd; + lkpi_usec2hz_max = -1ULL / lkpi_usec2hz_rem; + + gcd = lkpi_gcd_64(lkpi_msec2hz_rem, lkpi_msec2hz_div); + lkpi_msec2hz_rem /= gcd; + lkpi_msec2hz_div /= gcd; + lkpi_msec2hz_max = -1ULL / lkpi_msec2hz_rem; } SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);