Commit 6e94d1ef37e439bf45659cc071553574ccb98080

Authored by Daniel Borkmann
Committed by David S. Miller
1 parent cf27014866

net: socket: move ktime2ts to ktime header api

Currently, ktime2ts is a small helper function that is only used in
net/socket.c. Move this helper into the ktime API as a small inline
function, so that i) it's maintained together with ktime routines,
and ii) also other files can make use of it. The function is named
ktime_to_timespec_cond() and placed into the generic part of ktime,
since we internally make use of ktime_to_timespec(). ktime_to_timespec()
itself does not check the ktime variable for zero, hence, we name
this function ktime_to_timespec_cond() for only a conditional
conversion, and adapt its users to it.

Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Showing 2 changed files with 22 additions and 16 deletions Side-by-side Diff

include/linux/ktime.h
... ... @@ -330,6 +330,24 @@
330 330  
331 331 extern ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs);
332 332  
  333 +/**
  334 + * ktime_to_timespec_cond - convert a ktime_t variable to timespec
  335 + * format only if the variable contains data
  336 + * @kt: the ktime_t variable to convert
  337 + * @ts: the timespec variable to store the result in
  338 + *
  339 + * Returns true if there was a successful conversion, false if kt was 0.
  340 + */
  341 +static inline bool ktime_to_timespec_cond(const ktime_t kt, struct timespec *ts)
  342 +{
  343 + if (kt.tv64) {
  344 + *ts = ktime_to_timespec(kt);
  345 + return true;
  346 + } else {
  347 + return false;
  348 + }
  349 +}
  350 +
333 351 /*
334 352 * The resolution of the clocks. The resolution value is returned in
335 353 * the clock_getres() system call to give application programmers an
... ... @@ -681,16 +681,6 @@
681 681 }
682 682 EXPORT_SYMBOL(kernel_sendmsg);
683 683  
684   -static int ktime2ts(ktime_t kt, struct timespec *ts)
685   -{
686   - if (kt.tv64) {
687   - *ts = ktime_to_timespec(kt);
688   - return 1;
689   - } else {
690   - return 0;
691   - }
692   -}
693   -
694 684 /*
695 685 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
696 686 */
697 687  
698 688  
699 689  
... ... @@ -723,17 +713,15 @@
723 713  
724 714  
725 715 memset(ts, 0, sizeof(ts));
726   - if (skb->tstamp.tv64 &&
727   - sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
728   - skb_get_timestampns(skb, ts + 0);
  716 + if (sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE) &&
  717 + ktime_to_timespec_cond(skb->tstamp, ts + 0))
729 718 empty = 0;
730   - }
731 719 if (shhwtstamps) {
732 720 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
733   - ktime2ts(shhwtstamps->syststamp, ts + 1))
  721 + ktime_to_timespec_cond(shhwtstamps->syststamp, ts + 1))
734 722 empty = 0;
735 723 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
736   - ktime2ts(shhwtstamps->hwtstamp, ts + 2))
  724 + ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts + 2))
737 725 empty = 0;
738 726 }
739 727 if (!empty)