LCOV - code coverage report
Current view: top level - include/linux - time64.h (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 17 19 89.5 %
Date: 2020-09-30 20:25:40 Functions: 0 0 -
Branches: 40 64 62.5 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: GPL-2.0 */
       2                 :            : #ifndef _LINUX_TIME64_H
       3                 :            : #define _LINUX_TIME64_H
       4                 :            : 
       5                 :            : #include <linux/math64.h>
       6                 :            : 
       7                 :            : typedef __s64 time64_t;
       8                 :            : typedef __u64 timeu64_t;
       9                 :            : 
      10                 :            : #include <uapi/linux/time.h>
      11                 :            : 
      12                 :            : struct timespec64 {
      13                 :            :         time64_t        tv_sec;                 /* seconds */
      14                 :            :         long            tv_nsec;                /* nanoseconds */
      15                 :            : };
      16                 :            : 
      17                 :            : struct itimerspec64 {
      18                 :            :         struct timespec64 it_interval;
      19                 :            :         struct timespec64 it_value;
      20                 :            : };
      21                 :            : 
      22                 :            : /* Parameters used to convert the timespec values: */
      23                 :            : #define MSEC_PER_SEC    1000L
      24                 :            : #define USEC_PER_MSEC   1000L
      25                 :            : #define NSEC_PER_USEC   1000L
      26                 :            : #define NSEC_PER_MSEC   1000000L
      27                 :            : #define USEC_PER_SEC    1000000L
      28                 :            : #define NSEC_PER_SEC    1000000000L
      29                 :            : #define FSEC_PER_SEC    1000000000000000LL
      30                 :            : 
      31                 :            : /* Located here for timespec[64]_valid_strict */
      32                 :            : #define TIME64_MAX                      ((s64)~((u64)1 << 63))
      33                 :            : #define TIME64_MIN                      (-TIME64_MAX - 1)
      34                 :            : 
      35                 :            : #define KTIME_MAX                       ((s64)~((u64)1 << 63))
      36                 :            : #define KTIME_SEC_MAX                   (KTIME_MAX / NSEC_PER_SEC)
      37                 :            : 
      38                 :            : /*
      39                 :            :  * Limits for settimeofday():
      40                 :            :  *
      41                 :            :  * To prevent setting the time close to the wraparound point time setting
      42                 :            :  * is limited so a reasonable uptime can be accomodated. Uptime of 30 years
      43                 :            :  * should be really sufficient, which means the cutoff is 2232. At that
      44                 :            :  * point the cutoff is just a small part of the larger problem.
      45                 :            :  */
      46                 :            : #define TIME_UPTIME_SEC_MAX             (30LL * 365 * 24 *3600)
      47                 :            : #define TIME_SETTOD_SEC_MAX             (KTIME_SEC_MAX - TIME_UPTIME_SEC_MAX)
      48                 :            : 
      49                 :            : static inline int timespec64_equal(const struct timespec64 *a,
      50                 :            :                                    const struct timespec64 *b)
      51                 :            : {
      52   [ +  +  +  +  :    4208155 :         return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
          +  +  +  +  +  
          +  +  +  #  #  
                   #  # ]
      53                 :            : }
      54                 :            : 
      55                 :            : /*
      56                 :            :  * lhs < rhs:  return <0
      57                 :            :  * lhs == rhs: return 0
      58                 :            :  * lhs > rhs:  return >0
      59                 :            :  */
      60                 :            : static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
      61                 :            : {
      62   [ +  +  +  +  :    3220319 :         if (lhs->tv_sec < rhs->tv_sec)
             -  +  -  + ]
      63                 :            :                 return -1;
      64   [ +  +  +  +  :    1091911 :         if (lhs->tv_sec > rhs->tv_sec)
             #  #  #  # ]
      65                 :            :                 return 1;
      66                 :    1090874 :         return lhs->tv_nsec - rhs->tv_nsec;
      67                 :            : }
      68                 :            : 
      69                 :            : extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
      70                 :            : 
      71                 :            : static inline struct timespec64 timespec64_add(struct timespec64 lhs,
      72                 :            :                                                 struct timespec64 rhs)
      73                 :            : {
      74                 :            :         struct timespec64 ts_delta;
      75                 :        414 :         set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
      76                 :        207 :                                 lhs.tv_nsec + rhs.tv_nsec);
      77                 :        207 :         return ts_delta;
      78                 :            : }
      79                 :            : 
      80                 :            : /*
      81                 :            :  * sub = lhs - rhs, in normalized form
      82                 :            :  */
      83                 :            : static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
      84                 :            :                                                 struct timespec64 rhs)
      85                 :            : {
      86                 :            :         struct timespec64 ts_delta;
      87                 :    1065292 :         set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
      88                 :     532646 :                                 lhs.tv_nsec - rhs.tv_nsec);
      89                 :     532576 :         return ts_delta;
      90                 :            : }
      91                 :            : 
      92                 :            : /*
      93                 :            :  * Returns true if the timespec64 is norm, false if denorm:
      94                 :            :  */
      95                 :            : static inline bool timespec64_valid(const struct timespec64 *ts)
      96                 :            : {
      97                 :            :         /* Dates before 1970 are bogus */
      98   [ +  +  +  +  :     857598 :         if (ts->tv_sec < 0)
             +  -  +  - ]
      99                 :            :                 return false;
     100                 :            :         /* Can't have more nanoseconds then a second */
     101   [ +  +  +  +  :     857753 :         if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
             +  -  +  - ]
     102                 :            :                 return false;
     103                 :            :         return true;
     104                 :            : }
     105                 :            : 
     106                 :            : static inline bool timespec64_valid_strict(const struct timespec64 *ts)
     107                 :            : {
     108         [ #  # ]:          0 :         if (!timespec64_valid(ts))
     109                 :            :                 return false;
     110                 :            :         /* Disallow values that could overflow ktime_t */
     111         [ #  # ]:          0 :         if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
     112                 :            :                 return false;
     113                 :            :         return true;
     114                 :            : }
     115                 :            : 
     116                 :            : static inline bool timespec64_valid_settod(const struct timespec64 *ts)
     117                 :            : {
     118   [ +  -  +  -  :       1242 :         if (!timespec64_valid(ts))
                   +  - ]
     119                 :            :                 return false;
     120                 :            :         /* Disallow values which cause overflow issues vs. CLOCK_REALTIME */
     121   [ +  -  +  -  :       1242 :         if ((unsigned long long)ts->tv_sec >= TIME_SETTOD_SEC_MAX)
                   +  - ]
     122                 :            :                 return false;
     123                 :            :         return true;
     124                 :            : }
     125                 :            : 
     126                 :            : /**
     127                 :            :  * timespec64_to_ns - Convert timespec64 to nanoseconds
     128                 :            :  * @ts:         pointer to the timespec64 variable to be converted
     129                 :            :  *
     130                 :            :  * Returns the scalar nanosecond representation of the timespec64
     131                 :            :  * parameter.
     132                 :            :  */
     133                 :            : static inline s64 timespec64_to_ns(const struct timespec64 *ts)
     134                 :            : {
     135                 :        414 :         return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
     136                 :            : }
     137                 :            : 
     138                 :            : /**
     139                 :            :  * ns_to_timespec64 - Convert nanoseconds to timespec64
     140                 :            :  * @nsec:       the nanoseconds value to be converted
     141                 :            :  *
     142                 :            :  * Returns the timespec64 representation of the nsec parameter.
     143                 :            :  */
     144                 :            : extern struct timespec64 ns_to_timespec64(const s64 nsec);
     145                 :            : 
     146                 :            : /**
     147                 :            :  * timespec64_add_ns - Adds nanoseconds to a timespec64
     148                 :            :  * @a:          pointer to timespec64 to be incremented
     149                 :            :  * @ns:         unsigned nanoseconds value to be added
     150                 :            :  *
     151                 :            :  * This must always be inlined because its used from the x86-64 vdso,
     152                 :            :  * which cannot call other kernel functions.
     153                 :            :  */
     154                 :            : static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
     155                 :            : {
     156                 :    1356161 :         a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
     157                 :    1194270 :         a->tv_nsec = ns;
     158                 :            : }
     159                 :            : 
     160                 :            : /*
     161                 :            :  * timespec64_add_safe assumes both values are positive and checks for
     162                 :            :  * overflow. It will return TIME64_MAX in case of overflow.
     163                 :            :  */
     164                 :            : extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
     165                 :            :                                          const struct timespec64 rhs);
     166                 :            : 
     167                 :            : #endif /* _LINUX_TIME64_H */

Generated by: LCOV version 1.14