linux-wpan.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
@ 2019-04-16 20:32 Arnd Bergmann
  2019-04-17  9:35 ` Neil Horman
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Arnd Bergmann @ 2019-04-16 20:32 UTC (permalink / raw)
  To: David S. Miller, Ralf Baechle, Marcel Holtmann, Johan Hedberg,
	Oliver Hartkopp, Marc Kleine-Budde, Gerrit Renker,
	Alexander Aring, Stefan Schmidt, Alexey Kuznetsov,
	Hideaki YOSHIFUJI, Vlad Yasevich, Neil Horman,
	Marcelo Ricardo Leitner, Andrew Hendry, Arnd Bergmann,
	Eric Dumazet, Willem de Bruijn, Deepa Dinamani
  Cc: netdev, linux-kernel, linux-hams, linux-bluetooth, linux-can,
	dccp, linux-wpan, linux-sctp, linux-x25

The SIOCGSTAMP/SIOCGSTAMPNS ioctl commands are implemented by many
socket protocol handlers, and all of those end up calling the same
sock_get_timestamp()/sock_get_timestampns() helper functions, which
results in a lot of duplicate code.

With the introduction of 64-bit time_t on 32-bit architectures, this
gets worse, as we then need four different ioctl commands in each
socket protocol implementation.

To simplify that, let's add a new .gettstamp() operation in
struct proto_ops, and move ioctl implementation into the common
sock_ioctl()/compat_sock_ioctl_trans() functions that these all go
through.

We can reuse the sock_get_timestamp() implementation, but generalize
it so it can deal with both native and compat mode, as well as
timeval and timespec structures.

Acked-by: Stefan Schmidt <stefan@datenfreihafen.org>
Link: https://lore.kernel.org/lkml/CAK8P3a038aDQQotzua_QtKGhq8O9n+rdiz2=WDCp82ys8eUT+A@mail.gmail.com/
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
v2: reworked to not break sparc64 support
---
 include/linux/net.h          |  2 ++
 include/net/compat.h         |  3 --
 include/net/sock.h           |  4 +--
 net/appletalk/ddp.c          |  7 +----
 net/atm/ioctl.c              | 16 ----------
 net/atm/pvc.c                |  1 +
 net/atm/svc.c                |  1 +
 net/ax25/af_ax25.c           |  9 +-----
 net/bluetooth/af_bluetooth.c |  8 -----
 net/bluetooth/l2cap_sock.c   |  1 +
 net/bluetooth/rfcomm/sock.c  |  1 +
 net/bluetooth/sco.c          |  1 +
 net/can/af_can.c             |  6 ----
 net/can/bcm.c                |  1 +
 net/can/raw.c                |  1 +
 net/compat.c                 | 57 ------------------------------------
 net/core/sock.c              | 51 +++++++++++++++++---------------
 net/dccp/ipv4.c              |  1 +
 net/dccp/ipv6.c              |  1 +
 net/ieee802154/socket.c      |  6 ++--
 net/ipv4/af_inet.c           |  9 ++----
 net/ipv6/af_inet6.c          |  8 ++---
 net/ipv6/raw.c               |  1 +
 net/l2tp/l2tp_ip.c           |  1 +
 net/l2tp/l2tp_ip6.c          |  1 +
 net/netrom/af_netrom.c       | 14 +--------
 net/packet/af_packet.c       |  7 ++---
 net/qrtr/qrtr.c              |  4 +--
 net/rose/af_rose.c           |  7 +----
 net/sctp/ipv6.c              |  1 +
 net/sctp/protocol.c          |  1 +
 net/socket.c                 | 48 +++++++++---------------------
 net/x25/af_x25.c             | 27 +----------------
 33 files changed, 75 insertions(+), 232 deletions(-)

diff --git a/include/linux/net.h b/include/linux/net.h
index c606c72311d0..50bf5206ead6 100644
--- a/include/linux/net.h
+++ b/include/linux/net.h
@@ -161,6 +161,8 @@ struct proto_ops {
 	int	 	(*compat_ioctl) (struct socket *sock, unsigned int cmd,
 				      unsigned long arg);
 #endif
+	int		(*gettstamp) (struct socket *sock, void __user *userstamp,
+				      bool timeval, bool time32);
 	int		(*listen)    (struct socket *sock, int len);
 	int		(*shutdown)  (struct socket *sock, int flags);
 	int		(*setsockopt)(struct socket *sock, int level,
diff --git a/include/net/compat.h b/include/net/compat.h
index 4c6d75612b6c..f277653c7e17 100644
--- a/include/net/compat.h
+++ b/include/net/compat.h
@@ -30,9 +30,6 @@ struct compat_cmsghdr {
 	compat_int_t	cmsg_type;
 };
 
-int compat_sock_get_timestamp(struct sock *, struct timeval __user *);
-int compat_sock_get_timestampns(struct sock *, struct timespec __user *);
-
 #else /* defined(CONFIG_COMPAT) */
 /*
  * To avoid compiler warnings:
diff --git a/include/net/sock.h b/include/net/sock.h
index 8de5ee258b93..d1fe105dcf42 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1607,6 +1607,8 @@ int sock_setsockopt(struct socket *sock, int level, int op,
 
 int sock_getsockopt(struct socket *sock, int level, int op,
 		    char __user *optval, int __user *optlen);
+int sock_gettstamp(struct socket *sock, void __user *userstamp,
+		   bool timeval, bool time32);
 struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
 				    int noblock, int *errcode);
 struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
@@ -2493,8 +2495,6 @@ static inline bool sk_listener(const struct sock *sk)
 }
 
 void sock_enable_timestamp(struct sock *sk, int flag);
-int sock_get_timestamp(struct sock *, struct timeval __user *);
-int sock_get_timestampns(struct sock *, struct timespec __user *);
 int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len, int level,
 		       int type);
 
diff --git a/net/appletalk/ddp.c b/net/appletalk/ddp.c
index 709d2542f729..e2511027d19b 100644
--- a/net/appletalk/ddp.c
+++ b/net/appletalk/ddp.c
@@ -1806,12 +1806,6 @@ static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		rc = put_user(amount, (int __user *)argp);
 		break;
 	}
-	case SIOCGSTAMP:
-		rc = sock_get_timestamp(sk, argp);
-		break;
-	case SIOCGSTAMPNS:
-		rc = sock_get_timestampns(sk, argp);
-		break;
 	/* Routing */
 	case SIOCADDRT:
 	case SIOCDELRT:
@@ -1871,6 +1865,7 @@ static const struct proto_ops atalk_dgram_ops = {
 	.getname	= atalk_getname,
 	.poll		= datagram_poll,
 	.ioctl		= atalk_ioctl,
+	.gettstamp	= sock_gettstamp,
 #ifdef CONFIG_COMPAT
 	.compat_ioctl	= atalk_compat_ioctl,
 #endif
diff --git a/net/atm/ioctl.c b/net/atm/ioctl.c
index 2ff0e5e470e3..d955b683aa7c 100644
--- a/net/atm/ioctl.c
+++ b/net/atm/ioctl.c
@@ -81,22 +81,6 @@ static int do_vcc_ioctl(struct socket *sock, unsigned int cmd,
 				 (int __user *)argp) ? -EFAULT : 0;
 		goto done;
 	}
-	case SIOCGSTAMP: /* borrowed from IP */
-#ifdef CONFIG_COMPAT
-		if (compat)
-			error = compat_sock_get_timestamp(sk, argp);
-		else
-#endif
-			error = sock_get_timestamp(sk, argp);
-		goto done;
-	case SIOCGSTAMPNS: /* borrowed from IP */
-#ifdef CONFIG_COMPAT
-		if (compat)
-			error = compat_sock_get_timestampns(sk, argp);
-		else
-#endif
-			error = sock_get_timestampns(sk, argp);
-		goto done;
 	case ATM_SETSC:
 		net_warn_ratelimited("ATM_SETSC is obsolete; used by %s:%d\n",
 				     current->comm, task_pid_nr(current));
diff --git a/net/atm/pvc.c b/net/atm/pvc.c
index 2cb10af16afc..02bd2a436bdf 100644
--- a/net/atm/pvc.c
+++ b/net/atm/pvc.c
@@ -118,6 +118,7 @@ static const struct proto_ops pvc_proto_ops = {
 #ifdef CONFIG_COMPAT
 	.compat_ioctl = vcc_compat_ioctl,
 #endif
+	.gettstamp =	sock_gettstamp,
 	.listen =	sock_no_listen,
 	.shutdown =	pvc_shutdown,
 	.setsockopt =	pvc_setsockopt,
diff --git a/net/atm/svc.c b/net/atm/svc.c
index 2f91b766ac42..908cbb8654f5 100644
--- a/net/atm/svc.c
+++ b/net/atm/svc.c
@@ -641,6 +641,7 @@ static const struct proto_ops svc_proto_ops = {
 #ifdef CONFIG_COMPAT
 	.compat_ioctl =	svc_compat_ioctl,
 #endif
+	.gettstamp =	sock_gettstamp,
 	.listen =	svc_listen,
 	.shutdown =	svc_shutdown,
 	.setsockopt =	svc_setsockopt,
diff --git a/net/ax25/af_ax25.c b/net/ax25/af_ax25.c
index 5d01edf8d819..449e7b7190c1 100644
--- a/net/ax25/af_ax25.c
+++ b/net/ax25/af_ax25.c
@@ -1714,14 +1714,6 @@ static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		break;
 	}
 
-	case SIOCGSTAMP:
-		res = sock_get_timestamp(sk, argp);
-		break;
-
-	case SIOCGSTAMPNS:
-		res = sock_get_timestampns(sk, argp);
-		break;
-
 	case SIOCAX25ADDUID:	/* Add a uid to the uid/call map table */
 	case SIOCAX25DELUID:	/* Delete a uid from the uid/call map table */
 	case SIOCAX25GETUID: {
@@ -1950,6 +1942,7 @@ static const struct proto_ops ax25_proto_ops = {
 	.getname	= ax25_getname,
 	.poll		= datagram_poll,
 	.ioctl		= ax25_ioctl,
+	.gettstamp	= sock_gettstamp,
 	.listen		= ax25_listen,
 	.shutdown	= ax25_shutdown,
 	.setsockopt	= ax25_setsockopt,
diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
index 8d12198eaa94..94ddf19998c7 100644
--- a/net/bluetooth/af_bluetooth.c
+++ b/net/bluetooth/af_bluetooth.c
@@ -521,14 +521,6 @@ int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		err = put_user(amount, (int __user *) arg);
 		break;
 
-	case SIOCGSTAMP:
-		err = sock_get_timestamp(sk, (struct timeval __user *) arg);
-		break;
-
-	case SIOCGSTAMPNS:
-		err = sock_get_timestampns(sk, (struct timespec __user *) arg);
-		break;
-
 	default:
 		err = -ENOIOCTLCMD;
 		break;
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index a3a2cd55e23a..dcb14abebeba 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1655,6 +1655,7 @@ static const struct proto_ops l2cap_sock_ops = {
 	.recvmsg	= l2cap_sock_recvmsg,
 	.poll		= bt_sock_poll,
 	.ioctl		= bt_sock_ioctl,
+	.gettstamp	= sock_gettstamp,
 	.mmap		= sock_no_mmap,
 	.socketpair	= sock_no_socketpair,
 	.shutdown	= l2cap_sock_shutdown,
diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index b1f49fcc0478..90bb53aa4bee 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -1039,6 +1039,7 @@ static const struct proto_ops rfcomm_sock_ops = {
 	.setsockopt	= rfcomm_sock_setsockopt,
 	.getsockopt	= rfcomm_sock_getsockopt,
 	.ioctl		= rfcomm_sock_ioctl,
+	.gettstamp	= sock_gettstamp,
 	.poll		= bt_sock_poll,
 	.socketpair	= sock_no_socketpair,
 	.mmap		= sock_no_mmap
diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c
index 9a580999ca57..d894406a5c3b 100644
--- a/net/bluetooth/sco.c
+++ b/net/bluetooth/sco.c
@@ -1190,6 +1190,7 @@ static const struct proto_ops sco_sock_ops = {
 	.recvmsg	= sco_sock_recvmsg,
 	.poll		= bt_sock_poll,
 	.ioctl		= bt_sock_ioctl,
+	.gettstamp	= sock_gettstamp,
 	.mmap		= sock_no_mmap,
 	.socketpair	= sock_no_socketpair,
 	.shutdown	= sco_sock_shutdown,
diff --git a/net/can/af_can.c b/net/can/af_can.c
index 1684ba5b51eb..e8fd5dc1780a 100644
--- a/net/can/af_can.c
+++ b/net/can/af_can.c
@@ -89,13 +89,7 @@ static atomic_t skbcounter = ATOMIC_INIT(0);
 
 int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 {
-	struct sock *sk = sock->sk;
-
 	switch (cmd) {
-
-	case SIOCGSTAMP:
-		return sock_get_timestamp(sk, (struct timeval __user *)arg);
-
 	default:
 		return -ENOIOCTLCMD;
 	}
diff --git a/net/can/bcm.c b/net/can/bcm.c
index 79bb8afa9c0c..a34ee52f19ea 100644
--- a/net/can/bcm.c
+++ b/net/can/bcm.c
@@ -1689,6 +1689,7 @@ static const struct proto_ops bcm_ops = {
 	.getname       = sock_no_getname,
 	.poll          = datagram_poll,
 	.ioctl         = can_ioctl,	/* use can_ioctl() from af_can.c */
+	.gettstamp     = sock_gettstamp,
 	.listen        = sock_no_listen,
 	.shutdown      = sock_no_shutdown,
 	.setsockopt    = sock_no_setsockopt,
diff --git a/net/can/raw.c b/net/can/raw.c
index c70207537488..afcbff063a67 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -846,6 +846,7 @@ static const struct proto_ops raw_ops = {
 	.getname       = raw_getname,
 	.poll          = datagram_poll,
 	.ioctl         = can_ioctl,	/* use can_ioctl() from af_can.c */
+	.gettstamp     = sock_gettstamp,
 	.listen        = sock_no_listen,
 	.shutdown      = sock_no_shutdown,
 	.setsockopt    = raw_setsockopt,
diff --git a/net/compat.c b/net/compat.c
index eeea5eb71639..a031bd333092 100644
--- a/net/compat.c
+++ b/net/compat.c
@@ -395,63 +395,6 @@ COMPAT_SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
 	return __compat_sys_setsockopt(fd, level, optname, optval, optlen);
 }
 
-int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
-{
-	struct compat_timeval __user *ctv;
-	int err;
-	struct timeval tv;
-
-	if (COMPAT_USE_64BIT_TIME)
-		return sock_get_timestamp(sk, userstamp);
-
-	ctv = (struct compat_timeval __user *) userstamp;
-	err = -ENOENT;
-	sock_enable_timestamp(sk, SOCK_TIMESTAMP);
-	tv = ktime_to_timeval(sock_read_timestamp(sk));
-
-	if (tv.tv_sec == -1)
-		return err;
-	if (tv.tv_sec == 0) {
-		ktime_t kt = ktime_get_real();
-		sock_write_timestamp(sk, kt);
-		tv = ktime_to_timeval(kt);
-	}
-	err = 0;
-	if (put_user(tv.tv_sec, &ctv->tv_sec) ||
-			put_user(tv.tv_usec, &ctv->tv_usec))
-		err = -EFAULT;
-	return err;
-}
-EXPORT_SYMBOL(compat_sock_get_timestamp);
-
-int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
-{
-	struct compat_timespec __user *ctv;
-	int err;
-	struct timespec ts;
-
-	if (COMPAT_USE_64BIT_TIME)
-		return sock_get_timestampns (sk, userstamp);
-
-	ctv = (struct compat_timespec __user *) userstamp;
-	err = -ENOENT;
-	sock_enable_timestamp(sk, SOCK_TIMESTAMP);
-	ts = ktime_to_timespec(sock_read_timestamp(sk));
-	if (ts.tv_sec == -1)
-		return err;
-	if (ts.tv_sec == 0) {
-		ktime_t kt = ktime_get_real();
-		sock_write_timestamp(sk, kt);
-		ts = ktime_to_timespec(kt);
-	}
-	err = 0;
-	if (put_user(ts.tv_sec, &ctv->tv_sec) ||
-			put_user(ts.tv_nsec, &ctv->tv_nsec))
-		err = -EFAULT;
-	return err;
-}
-EXPORT_SYMBOL(compat_sock_get_timestampns);
-
 static int __compat_sys_getsockopt(int fd, int level, int optname,
 				   char __user *optval,
 				   int __user *optlen)
diff --git a/net/core/sock.c b/net/core/sock.c
index 782343bb925b..dc7c31d66200 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -2977,39 +2977,44 @@ bool lock_sock_fast(struct sock *sk)
 }
 EXPORT_SYMBOL(lock_sock_fast);
 
-int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
+int sock_gettstamp(struct socket *sock, void __user *userstamp,
+		   bool timeval, bool time32)
 {
-	struct timeval tv;
+	struct sock *sk = sock->sk;
+	struct timespec64 ts;
 
 	sock_enable_timestamp(sk, SOCK_TIMESTAMP);
-	tv = ktime_to_timeval(sock_read_timestamp(sk));
-	if (tv.tv_sec == -1)
+	ts = ktime_to_timespec64(sk->sk_stamp);
+	if (ts.tv_sec == -1)
 		return -ENOENT;
-	if (tv.tv_sec == 0) {
+	if (ts.tv_sec == 0) {
 		ktime_t kt = ktime_get_real();
-		sock_write_timestamp(sk, kt);
-		tv = ktime_to_timeval(kt);
+		sock_write_timestamp(sk, kt);;
+		ts = ktime_to_timespec64(kt);
 	}
-	return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
-}
-EXPORT_SYMBOL(sock_get_timestamp);
 
-int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
-{
-	struct timespec ts;
+	if (timeval)
+		ts.tv_nsec /= 1000;
 
-	sock_enable_timestamp(sk, SOCK_TIMESTAMP);
-	ts = ktime_to_timespec(sock_read_timestamp(sk));
-	if (ts.tv_sec == -1)
-		return -ENOENT;
-	if (ts.tv_sec == 0) {
-		ktime_t kt = ktime_get_real();
-		sock_write_timestamp(sk, kt);
-		ts = ktime_to_timespec(sk->sk_stamp);
+#ifdef CONFIG_COMPAT_32BIT_TIME
+	if (time32)
+		return put_old_timespec32(&ts, userstamp);
+#endif
+#ifdef CONFIG_SPARC64
+	/* beware of padding in sparc64 timeval */
+	if (timeval && !in_compat_syscall()) {
+		struct __kernel_old_timeval __user tv = {
+			.tv_sec = ts.tv_sec;
+			.tv_usec = ts.tv_nsec;
+		};
+		if (copy_to_user(userstamp, &tv, sizeof(tv))
+			return -EFAULT;
+		return 0;
 	}
-	return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
+#endif
+	return put_timespec64(&ts, userstamp);
 }
-EXPORT_SYMBOL(sock_get_timestampns);
+EXPORT_SYMBOL(sock_gettstamp);
 
 void sock_enable_timestamp(struct sock *sk, int flag)
 {
diff --git a/net/dccp/ipv4.c b/net/dccp/ipv4.c
index 26a21d97b6b0..004535e4c070 100644
--- a/net/dccp/ipv4.c
+++ b/net/dccp/ipv4.c
@@ -991,6 +991,7 @@ static const struct proto_ops inet_dccp_ops = {
 	/* FIXME: work on tcp_poll to rename it to inet_csk_poll */
 	.poll		   = dccp_poll,
 	.ioctl		   = inet_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	/* FIXME: work on inet_listen to rename it to sock_common_listen */
 	.listen		   = inet_dccp_listen,
 	.shutdown	   = inet_shutdown,
diff --git a/net/dccp/ipv6.c b/net/dccp/ipv6.c
index 57d84e9b7b6f..c4e4d1301062 100644
--- a/net/dccp/ipv6.c
+++ b/net/dccp/ipv6.c
@@ -1075,6 +1075,7 @@ static const struct proto_ops inet6_dccp_ops = {
 	.getname	   = inet6_getname,
 	.poll		   = dccp_poll,
 	.ioctl		   = inet6_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = inet_dccp_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
diff --git a/net/ieee802154/socket.c b/net/ieee802154/socket.c
index bc6b912603f1..ce2dfb997537 100644
--- a/net/ieee802154/socket.c
+++ b/net/ieee802154/socket.c
@@ -164,10 +164,6 @@ static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
 	struct sock *sk = sock->sk;
 
 	switch (cmd) {
-	case SIOCGSTAMP:
-		return sock_get_timestamp(sk, (struct timeval __user *)arg);
-	case SIOCGSTAMPNS:
-		return sock_get_timestampns(sk, (struct timespec __user *)arg);
 	case SIOCGIFADDR:
 	case SIOCSIFADDR:
 		return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
@@ -426,6 +422,7 @@ static const struct proto_ops ieee802154_raw_ops = {
 	.getname	   = sock_no_getname,
 	.poll		   = datagram_poll,
 	.ioctl		   = ieee802154_sock_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,
 	.shutdown	   = sock_no_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
@@ -988,6 +985,7 @@ static const struct proto_ops ieee802154_dgram_ops = {
 	.getname	   = sock_no_getname,
 	.poll		   = datagram_poll,
 	.ioctl		   = ieee802154_sock_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,
 	.shutdown	   = sock_no_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index eab3ebde981e..0f46d87715aa 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -911,12 +911,6 @@ int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 	struct rtentry rt;
 
 	switch (cmd) {
-	case SIOCGSTAMP:
-		err = sock_get_timestamp(sk, (struct timeval __user *)arg);
-		break;
-	case SIOCGSTAMPNS:
-		err = sock_get_timestampns(sk, (struct timespec __user *)arg);
-		break;
 	case SIOCADDRT:
 	case SIOCDELRT:
 		if (copy_from_user(&rt, p, sizeof(struct rtentry)))
@@ -988,6 +982,7 @@ const struct proto_ops inet_stream_ops = {
 	.getname	   = inet_getname,
 	.poll		   = tcp_poll,
 	.ioctl		   = inet_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = inet_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
@@ -1023,6 +1018,7 @@ const struct proto_ops inet_dgram_ops = {
 	.getname	   = inet_getname,
 	.poll		   = udp_poll,
 	.ioctl		   = inet_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
@@ -1055,6 +1051,7 @@ static const struct proto_ops inet_sockraw_ops = {
 	.getname	   = inet_getname,
 	.poll		   = datagram_poll,
 	.ioctl		   = inet_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
index 2f45d2a3e3a3..6d1133fd89f6 100644
--- a/net/ipv6/af_inet6.c
+++ b/net/ipv6/af_inet6.c
@@ -546,12 +546,6 @@ int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 	struct net *net = sock_net(sk);
 
 	switch (cmd) {
-	case SIOCGSTAMP:
-		return sock_get_timestamp(sk, (struct timeval __user *)arg);
-
-	case SIOCGSTAMPNS:
-		return sock_get_timestampns(sk, (struct timespec __user *)arg);
-
 	case SIOCADDRT:
 	case SIOCDELRT:
 
@@ -584,6 +578,7 @@ const struct proto_ops inet6_stream_ops = {
 	.getname	   = inet6_getname,
 	.poll		   = tcp_poll,			/* ok		*/
 	.ioctl		   = inet6_ioctl,		/* must change  */
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = inet_listen,		/* ok		*/
 	.shutdown	   = inet_shutdown,		/* ok		*/
 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
@@ -617,6 +612,7 @@ const struct proto_ops inet6_dgram_ops = {
 	.getname	   = inet6_getname,
 	.poll		   = udp_poll,			/* ok		*/
 	.ioctl		   = inet6_ioctl,		/* must change  */
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,		/* ok		*/
 	.shutdown	   = inet_shutdown,		/* ok		*/
 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c
index 5a426226c762..84dbe21b71e5 100644
--- a/net/ipv6/raw.c
+++ b/net/ipv6/raw.c
@@ -1356,6 +1356,7 @@ const struct proto_ops inet6_sockraw_ops = {
 	.getname	   = inet6_getname,
 	.poll		   = datagram_poll,		/* ok		*/
 	.ioctl		   = inet6_ioctl,		/* must change  */
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,		/* ok		*/
 	.shutdown	   = inet_shutdown,		/* ok		*/
 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
diff --git a/net/l2tp/l2tp_ip.c b/net/l2tp/l2tp_ip.c
index d4c60523c549..2cac910c1cd4 100644
--- a/net/l2tp/l2tp_ip.c
+++ b/net/l2tp/l2tp_ip.c
@@ -618,6 +618,7 @@ static const struct proto_ops l2tp_ip_ops = {
 	.getname	   = l2tp_ip_getname,
 	.poll		   = datagram_poll,
 	.ioctl		   = inet_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c
index 37a69df17cab..4ec546cc1dd6 100644
--- a/net/l2tp/l2tp_ip6.c
+++ b/net/l2tp/l2tp_ip6.c
@@ -752,6 +752,7 @@ static const struct proto_ops l2tp_ip6_ops = {
 	.getname	   = l2tp_ip6_getname,
 	.poll		   = datagram_poll,
 	.ioctl		   = inet6_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
diff --git a/net/netrom/af_netrom.c b/net/netrom/af_netrom.c
index 1d3144d19903..4e97bbbf1916 100644
--- a/net/netrom/af_netrom.c
+++ b/net/netrom/af_netrom.c
@@ -1199,7 +1199,6 @@ static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 {
 	struct sock *sk = sock->sk;
 	void __user *argp = (void __user *)arg;
-	int ret;
 
 	switch (cmd) {
 	case TIOCOUTQ: {
@@ -1225,18 +1224,6 @@ static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		return put_user(amount, (int __user *)argp);
 	}
 
-	case SIOCGSTAMP:
-		lock_sock(sk);
-		ret = sock_get_timestamp(sk, argp);
-		release_sock(sk);
-		return ret;
-
-	case SIOCGSTAMPNS:
-		lock_sock(sk);
-		ret = sock_get_timestampns(sk, argp);
-		release_sock(sk);
-		return ret;
-
 	case SIOCGIFADDR:
 	case SIOCSIFADDR:
 	case SIOCGIFDSTADDR:
@@ -1362,6 +1349,7 @@ static const struct proto_ops nr_proto_ops = {
 	.getname	=	nr_getname,
 	.poll		=	datagram_poll,
 	.ioctl		=	nr_ioctl,
+	.gettstamp	=	sock_gettstamp,
 	.listen		=	nr_listen,
 	.shutdown	=	sock_no_shutdown,
 	.setsockopt	=	nr_setsockopt,
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 9419c5cf4de5..b012dc5ffb6e 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -4077,11 +4077,6 @@ static int packet_ioctl(struct socket *sock, unsigned int cmd,
 		spin_unlock_bh(&sk->sk_receive_queue.lock);
 		return put_user(amount, (int __user *)arg);
 	}
-	case SIOCGSTAMP:
-		return sock_get_timestamp(sk, (struct timeval __user *)arg);
-	case SIOCGSTAMPNS:
-		return sock_get_timestampns(sk, (struct timespec __user *)arg);
-
 #ifdef CONFIG_INET
 	case SIOCADDRT:
 	case SIOCDELRT:
@@ -4457,6 +4452,7 @@ static const struct proto_ops packet_ops_spkt = {
 	.getname =	packet_getname_spkt,
 	.poll =		datagram_poll,
 	.ioctl =	packet_ioctl,
+	.gettstamp =	sock_gettstamp,
 	.listen =	sock_no_listen,
 	.shutdown =	sock_no_shutdown,
 	.setsockopt =	sock_no_setsockopt,
@@ -4478,6 +4474,7 @@ static const struct proto_ops packet_ops = {
 	.getname =	packet_getname,
 	.poll =		packet_poll,
 	.ioctl =	packet_ioctl,
+	.gettstamp =	sock_gettstamp,
 	.listen =	sock_no_listen,
 	.shutdown =	sock_no_shutdown,
 	.setsockopt =	packet_setsockopt,
diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
index b37e6e0a1026..7c5e8292cc0a 100644
--- a/net/qrtr/qrtr.c
+++ b/net/qrtr/qrtr.c
@@ -968,9 +968,6 @@ static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 			break;
 		}
 		break;
-	case SIOCGSTAMP:
-		rc = sock_get_timestamp(sk, argp);
-		break;
 	case SIOCADDRT:
 	case SIOCDELRT:
 	case SIOCSIFADDR:
@@ -1033,6 +1030,7 @@ static const struct proto_ops qrtr_proto_ops = {
 	.recvmsg	= qrtr_recvmsg,
 	.getname	= qrtr_getname,
 	.ioctl		= qrtr_ioctl,
+	.gettstamp	= sock_gettstamp,
 	.poll		= datagram_poll,
 	.shutdown	= sock_no_shutdown,
 	.setsockopt	= sock_no_setsockopt,
diff --git a/net/rose/af_rose.c b/net/rose/af_rose.c
index c96f63ffe31e..e274bc6e1458 100644
--- a/net/rose/af_rose.c
+++ b/net/rose/af_rose.c
@@ -1301,12 +1301,6 @@ static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		return put_user(amount, (unsigned int __user *) argp);
 	}
 
-	case SIOCGSTAMP:
-		return sock_get_timestamp(sk, (struct timeval __user *) argp);
-
-	case SIOCGSTAMPNS:
-		return sock_get_timestampns(sk, (struct timespec __user *) argp);
-
 	case SIOCGIFADDR:
 	case SIOCSIFADDR:
 	case SIOCGIFDSTADDR:
@@ -1474,6 +1468,7 @@ static const struct proto_ops rose_proto_ops = {
 	.getname	=	rose_getname,
 	.poll		=	datagram_poll,
 	.ioctl		=	rose_ioctl,
+	.gettstamp	=	sock_gettstamp,
 	.listen		=	rose_listen,
 	.shutdown	=	sock_no_shutdown,
 	.setsockopt	=	rose_setsockopt,
diff --git a/net/sctp/ipv6.c b/net/sctp/ipv6.c
index 6200cd2b4b99..188c47eb206e 100644
--- a/net/sctp/ipv6.c
+++ b/net/sctp/ipv6.c
@@ -1030,6 +1030,7 @@ static const struct proto_ops inet6_seqpacket_ops = {
 	.getname	   = sctp_getname,
 	.poll		   = sctp_poll,
 	.ioctl		   = inet6_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sctp_inet_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
diff --git a/net/sctp/protocol.c b/net/sctp/protocol.c
index 951afdeea5e9..f0631bf486b6 100644
--- a/net/sctp/protocol.c
+++ b/net/sctp/protocol.c
@@ -1026,6 +1026,7 @@ static const struct proto_ops inet_seqpacket_ops = {
 	.getname	   = inet_getname,	/* Semantics are different.  */
 	.poll		   = sctp_poll,
 	.ioctl		   = inet_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sctp_inet_listen,
 	.shutdown	   = inet_shutdown,	/* Looks harmless.  */
 	.setsockopt	   = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
diff --git a/net/socket.c b/net/socket.c
index 8255f5bda0aa..ab624d42ead5 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -1164,6 +1164,15 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
 
 			err = open_related_ns(&net->ns, get_net_ns);
 			break;
+		case SIOCGSTAMP:
+		case SIOCGSTAMPNS:
+			if (!sock->ops->gettstamp) {
+				err = -ENOIOCTLCMD;
+				break;
+			}
+			err = sock->ops->gettstamp(sock, argp,
+						   cmd == SIOCGSTAMP, false);
+			break;
 		default:
 			err = sock_do_ioctl(net, sock, cmd, arg);
 			break;
@@ -2916,38 +2925,6 @@ void socket_seq_show(struct seq_file *seq)
 #endif				/* CONFIG_PROC_FS */
 
 #ifdef CONFIG_COMPAT
-static int do_siocgstamp(struct net *net, struct socket *sock,
-			 unsigned int cmd, void __user *up)
-{
-	mm_segment_t old_fs = get_fs();
-	struct timeval ktv;
-	int err;
-
-	set_fs(KERNEL_DS);
-	err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
-	set_fs(old_fs);
-	if (!err)
-		err = compat_put_timeval(&ktv, up);
-
-	return err;
-}
-
-static int do_siocgstampns(struct net *net, struct socket *sock,
-			   unsigned int cmd, void __user *up)
-{
-	mm_segment_t old_fs = get_fs();
-	struct timespec kts;
-	int err;
-
-	set_fs(KERNEL_DS);
-	err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
-	set_fs(old_fs);
-	if (!err)
-		err = compat_put_timespec(&kts, up);
-
-	return err;
-}
-
 static int compat_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
 {
 	struct compat_ifconf ifc32;
@@ -3348,9 +3325,12 @@ static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
 	case SIOCDELRT:
 		return routing_ioctl(net, sock, cmd, argp);
 	case SIOCGSTAMP:
-		return do_siocgstamp(net, sock, cmd, argp);
 	case SIOCGSTAMPNS:
-		return do_siocgstampns(net, sock, cmd, argp);
+		if (!sock->ops->gettstamp)
+			return -ENOIOCTLCMD;
+		return sock->ops->gettstamp(sock, argp, cmd == SIOCGSTAMP,
+					    !COMPAT_USE_64BIT_TIME);
+
 	case SIOCBONDSLAVEINFOQUERY:
 	case SIOCBONDINFOQUERY:
 	case SIOCSHWTSTAMP:
diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
index 20a511398389..0ea48a52ce79 100644
--- a/net/x25/af_x25.c
+++ b/net/x25/af_x25.c
@@ -1398,18 +1398,6 @@ static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		break;
 	}
 
-	case SIOCGSTAMP:
-		rc = -EINVAL;
-		if (sk)
-			rc = sock_get_timestamp(sk,
-						(struct timeval __user *)argp);
-		break;
-	case SIOCGSTAMPNS:
-		rc = -EINVAL;
-		if (sk)
-			rc = sock_get_timestampns(sk,
-					(struct timespec __user *)argp);
-		break;
 	case SIOCGIFADDR:
 	case SIOCSIFADDR:
 	case SIOCGIFDSTADDR:
@@ -1681,8 +1669,6 @@ static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
 				unsigned long arg)
 {
 	void __user *argp = compat_ptr(arg);
-	struct sock *sk = sock->sk;
-
 	int rc = -ENOIOCTLCMD;
 
 	switch(cmd) {
@@ -1690,18 +1676,6 @@ static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
 	case TIOCINQ:
 		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
 		break;
-	case SIOCGSTAMP:
-		rc = -EINVAL;
-		if (sk)
-			rc = compat_sock_get_timestamp(sk,
-					(struct timeval __user*)argp);
-		break;
-	case SIOCGSTAMPNS:
-		rc = -EINVAL;
-		if (sk)
-			rc = compat_sock_get_timestampns(sk,
-					(struct timespec __user*)argp);
-		break;
 	case SIOCGIFADDR:
 	case SIOCSIFADDR:
 	case SIOCGIFDSTADDR:
@@ -1765,6 +1739,7 @@ static const struct proto_ops x25_proto_ops = {
 #ifdef CONFIG_COMPAT
 	.compat_ioctl = compat_x25_ioctl,
 #endif
+	.gettstamp =	sock_gettstamp,
 	.listen =	x25_listen,
 	.shutdown =	sock_no_shutdown,
 	.setsockopt =	x25_setsockopt,
-- 
2.20.0

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2019-04-16 20:32 [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling Arnd Bergmann
@ 2019-04-17  9:35 ` Neil Horman
  2019-04-17 17:21   ` David Miller
  2019-04-17  9:59 ` Marc Kleine-Budde
  2019-04-17 14:46 ` Willem de Bruijn
  2 siblings, 1 reply; 16+ messages in thread
From: Neil Horman @ 2019-04-17  9:35 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: David S. Miller, Ralf Baechle, Marcel Holtmann, Johan Hedberg,
	Oliver Hartkopp, Marc Kleine-Budde, Gerrit Renker,
	Alexander Aring, Stefan Schmidt, Alexey Kuznetsov,
	Hideaki YOSHIFUJI, Vlad Yasevich, Marcelo Ricardo Leitner,
	Andrew Hendry, Eric Dumazet, Willem de Bruijn, Deepa Dinamani,
	netdev, linux-kernel, linux-hams, linux-bluetooth, linux-can,
	dccp, linux-wpan, linux-sctp, linux-x25

On Tue, Apr 16, 2019 at 10:32:48PM +0200, Arnd Bergmann wrote:
> The SIOCGSTAMP/SIOCGSTAMPNS ioctl commands are implemented by many
> socket protocol handlers, and all of those end up calling the same
> sock_get_timestamp()/sock_get_timestampns() helper functions, which
> results in a lot of duplicate code.
> 
> With the introduction of 64-bit time_t on 32-bit architectures, this
> gets worse, as we then need four different ioctl commands in each
> socket protocol implementation.
> 
> To simplify that, let's add a new .gettstamp() operation in
> struct proto_ops, and move ioctl implementation into the common
> sock_ioctl()/compat_sock_ioctl_trans() functions that these all go
> through.
> 
> We can reuse the sock_get_timestamp() implementation, but generalize
> it so it can deal with both native and compat mode, as well as
> timeval and timespec structures.
> 
> Acked-by: Stefan Schmidt <stefan@datenfreihafen.org>
> Link: https://lore.kernel.org/lkml/CAK8P3a038aDQQotzua_QtKGhq8O9n+rdiz2=WDCp82ys8eUT+A@mail.gmail.com/
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
> v2: reworked to not break sparc64 support
> ---
>  include/linux/net.h          |  2 ++
>  include/net/compat.h         |  3 --
>  include/net/sock.h           |  4 +--
>  net/appletalk/ddp.c          |  7 +----
>  net/atm/ioctl.c              | 16 ----------
>  net/atm/pvc.c                |  1 +
>  net/atm/svc.c                |  1 +
>  net/ax25/af_ax25.c           |  9 +-----
>  net/bluetooth/af_bluetooth.c |  8 -----
>  net/bluetooth/l2cap_sock.c   |  1 +
>  net/bluetooth/rfcomm/sock.c  |  1 +
>  net/bluetooth/sco.c          |  1 +
>  net/can/af_can.c             |  6 ----
>  net/can/bcm.c                |  1 +
>  net/can/raw.c                |  1 +
>  net/compat.c                 | 57 ------------------------------------
>  net/core/sock.c              | 51 +++++++++++++++++---------------
>  net/dccp/ipv4.c              |  1 +
>  net/dccp/ipv6.c              |  1 +
>  net/ieee802154/socket.c      |  6 ++--
>  net/ipv4/af_inet.c           |  9 ++----
>  net/ipv6/af_inet6.c          |  8 ++---
>  net/ipv6/raw.c               |  1 +
>  net/l2tp/l2tp_ip.c           |  1 +
>  net/l2tp/l2tp_ip6.c          |  1 +
>  net/netrom/af_netrom.c       | 14 +--------
>  net/packet/af_packet.c       |  7 ++---
>  net/qrtr/qrtr.c              |  4 +--
>  net/rose/af_rose.c           |  7 +----
>  net/sctp/ipv6.c              |  1 +
>  net/sctp/protocol.c          |  1 +
>  net/socket.c                 | 48 +++++++++---------------------
>  net/x25/af_x25.c             | 27 +----------------
>  33 files changed, 75 insertions(+), 232 deletions(-)
> 
> diff --git a/include/linux/net.h b/include/linux/net.h
> index c606c72311d0..50bf5206ead6 100644
> --- a/include/linux/net.h
> +++ b/include/linux/net.h
> @@ -161,6 +161,8 @@ struct proto_ops {
>  	int	 	(*compat_ioctl) (struct socket *sock, unsigned int cmd,
>  				      unsigned long arg);
>  #endif
> +	int		(*gettstamp) (struct socket *sock, void __user *userstamp,
> +				      bool timeval, bool time32);
>  	int		(*listen)    (struct socket *sock, int len);
>  	int		(*shutdown)  (struct socket *sock, int flags);
>  	int		(*setsockopt)(struct socket *sock, int level,
> diff --git a/include/net/compat.h b/include/net/compat.h
> index 4c6d75612b6c..f277653c7e17 100644
> --- a/include/net/compat.h
> +++ b/include/net/compat.h
> @@ -30,9 +30,6 @@ struct compat_cmsghdr {
>  	compat_int_t	cmsg_type;
>  };
>  
> -int compat_sock_get_timestamp(struct sock *, struct timeval __user *);
> -int compat_sock_get_timestampns(struct sock *, struct timespec __user *);
> -
>  #else /* defined(CONFIG_COMPAT) */
>  /*
>   * To avoid compiler warnings:
> diff --git a/include/net/sock.h b/include/net/sock.h
> index 8de5ee258b93..d1fe105dcf42 100644
> --- a/include/net/sock.h
> +++ b/include/net/sock.h
> @@ -1607,6 +1607,8 @@ int sock_setsockopt(struct socket *sock, int level, int op,
>  
>  int sock_getsockopt(struct socket *sock, int level, int op,
>  		    char __user *optval, int __user *optlen);
> +int sock_gettstamp(struct socket *sock, void __user *userstamp,
> +		   bool timeval, bool time32);
>  struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
>  				    int noblock, int *errcode);
>  struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
> @@ -2493,8 +2495,6 @@ static inline bool sk_listener(const struct sock *sk)
>  }
>  
>  void sock_enable_timestamp(struct sock *sk, int flag);
> -int sock_get_timestamp(struct sock *, struct timeval __user *);
> -int sock_get_timestampns(struct sock *, struct timespec __user *);
>  int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len, int level,
>  		       int type);
>  
> diff --git a/net/appletalk/ddp.c b/net/appletalk/ddp.c
> index 709d2542f729..e2511027d19b 100644
> --- a/net/appletalk/ddp.c
> +++ b/net/appletalk/ddp.c
> @@ -1806,12 +1806,6 @@ static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>  		rc = put_user(amount, (int __user *)argp);
>  		break;
>  	}
> -	case SIOCGSTAMP:
> -		rc = sock_get_timestamp(sk, argp);
> -		break;
> -	case SIOCGSTAMPNS:
> -		rc = sock_get_timestampns(sk, argp);
> -		break;
>  	/* Routing */
>  	case SIOCADDRT:
>  	case SIOCDELRT:
> @@ -1871,6 +1865,7 @@ static const struct proto_ops atalk_dgram_ops = {
>  	.getname	= atalk_getname,
>  	.poll		= datagram_poll,
>  	.ioctl		= atalk_ioctl,
> +	.gettstamp	= sock_gettstamp,
>  #ifdef CONFIG_COMPAT
>  	.compat_ioctl	= atalk_compat_ioctl,
>  #endif
> diff --git a/net/atm/ioctl.c b/net/atm/ioctl.c
> index 2ff0e5e470e3..d955b683aa7c 100644
> --- a/net/atm/ioctl.c
> +++ b/net/atm/ioctl.c
> @@ -81,22 +81,6 @@ static int do_vcc_ioctl(struct socket *sock, unsigned int cmd,
>  				 (int __user *)argp) ? -EFAULT : 0;
>  		goto done;
>  	}
> -	case SIOCGSTAMP: /* borrowed from IP */
> -#ifdef CONFIG_COMPAT
> -		if (compat)
> -			error = compat_sock_get_timestamp(sk, argp);
> -		else
> -#endif
> -			error = sock_get_timestamp(sk, argp);
> -		goto done;
> -	case SIOCGSTAMPNS: /* borrowed from IP */
> -#ifdef CONFIG_COMPAT
> -		if (compat)
> -			error = compat_sock_get_timestampns(sk, argp);
> -		else
> -#endif
> -			error = sock_get_timestampns(sk, argp);
> -		goto done;
>  	case ATM_SETSC:
>  		net_warn_ratelimited("ATM_SETSC is obsolete; used by %s:%d\n",
>  				     current->comm, task_pid_nr(current));
> diff --git a/net/atm/pvc.c b/net/atm/pvc.c
> index 2cb10af16afc..02bd2a436bdf 100644
> --- a/net/atm/pvc.c
> +++ b/net/atm/pvc.c
> @@ -118,6 +118,7 @@ static const struct proto_ops pvc_proto_ops = {
>  #ifdef CONFIG_COMPAT
>  	.compat_ioctl = vcc_compat_ioctl,
>  #endif
> +	.gettstamp =	sock_gettstamp,
>  	.listen =	sock_no_listen,
>  	.shutdown =	pvc_shutdown,
>  	.setsockopt =	pvc_setsockopt,
> diff --git a/net/atm/svc.c b/net/atm/svc.c
> index 2f91b766ac42..908cbb8654f5 100644
> --- a/net/atm/svc.c
> +++ b/net/atm/svc.c
> @@ -641,6 +641,7 @@ static const struct proto_ops svc_proto_ops = {
>  #ifdef CONFIG_COMPAT
>  	.compat_ioctl =	svc_compat_ioctl,
>  #endif
> +	.gettstamp =	sock_gettstamp,
>  	.listen =	svc_listen,
>  	.shutdown =	svc_shutdown,
>  	.setsockopt =	svc_setsockopt,
> diff --git a/net/ax25/af_ax25.c b/net/ax25/af_ax25.c
> index 5d01edf8d819..449e7b7190c1 100644
> --- a/net/ax25/af_ax25.c
> +++ b/net/ax25/af_ax25.c
> @@ -1714,14 +1714,6 @@ static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>  		break;
>  	}
>  
> -	case SIOCGSTAMP:
> -		res = sock_get_timestamp(sk, argp);
> -		break;
> -
> -	case SIOCGSTAMPNS:
> -		res = sock_get_timestampns(sk, argp);
> -		break;
> -
>  	case SIOCAX25ADDUID:	/* Add a uid to the uid/call map table */
>  	case SIOCAX25DELUID:	/* Delete a uid from the uid/call map table */
>  	case SIOCAX25GETUID: {
> @@ -1950,6 +1942,7 @@ static const struct proto_ops ax25_proto_ops = {
>  	.getname	= ax25_getname,
>  	.poll		= datagram_poll,
>  	.ioctl		= ax25_ioctl,
> +	.gettstamp	= sock_gettstamp,
>  	.listen		= ax25_listen,
>  	.shutdown	= ax25_shutdown,
>  	.setsockopt	= ax25_setsockopt,
> diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
> index 8d12198eaa94..94ddf19998c7 100644
> --- a/net/bluetooth/af_bluetooth.c
> +++ b/net/bluetooth/af_bluetooth.c
> @@ -521,14 +521,6 @@ int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>  		err = put_user(amount, (int __user *) arg);
>  		break;
>  
> -	case SIOCGSTAMP:
> -		err = sock_get_timestamp(sk, (struct timeval __user *) arg);
> -		break;
> -
> -	case SIOCGSTAMPNS:
> -		err = sock_get_timestampns(sk, (struct timespec __user *) arg);
> -		break;
> -
>  	default:
>  		err = -ENOIOCTLCMD;
>  		break;
> diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
> index a3a2cd55e23a..dcb14abebeba 100644
> --- a/net/bluetooth/l2cap_sock.c
> +++ b/net/bluetooth/l2cap_sock.c
> @@ -1655,6 +1655,7 @@ static const struct proto_ops l2cap_sock_ops = {
>  	.recvmsg	= l2cap_sock_recvmsg,
>  	.poll		= bt_sock_poll,
>  	.ioctl		= bt_sock_ioctl,
> +	.gettstamp	= sock_gettstamp,
>  	.mmap		= sock_no_mmap,
>  	.socketpair	= sock_no_socketpair,
>  	.shutdown	= l2cap_sock_shutdown,
> diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
> index b1f49fcc0478..90bb53aa4bee 100644
> --- a/net/bluetooth/rfcomm/sock.c
> +++ b/net/bluetooth/rfcomm/sock.c
> @@ -1039,6 +1039,7 @@ static const struct proto_ops rfcomm_sock_ops = {
>  	.setsockopt	= rfcomm_sock_setsockopt,
>  	.getsockopt	= rfcomm_sock_getsockopt,
>  	.ioctl		= rfcomm_sock_ioctl,
> +	.gettstamp	= sock_gettstamp,
>  	.poll		= bt_sock_poll,
>  	.socketpair	= sock_no_socketpair,
>  	.mmap		= sock_no_mmap
> diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c
> index 9a580999ca57..d894406a5c3b 100644
> --- a/net/bluetooth/sco.c
> +++ b/net/bluetooth/sco.c
> @@ -1190,6 +1190,7 @@ static const struct proto_ops sco_sock_ops = {
>  	.recvmsg	= sco_sock_recvmsg,
>  	.poll		= bt_sock_poll,
>  	.ioctl		= bt_sock_ioctl,
> +	.gettstamp	= sock_gettstamp,
>  	.mmap		= sock_no_mmap,
>  	.socketpair	= sock_no_socketpair,
>  	.shutdown	= sco_sock_shutdown,
> diff --git a/net/can/af_can.c b/net/can/af_can.c
> index 1684ba5b51eb..e8fd5dc1780a 100644
> --- a/net/can/af_can.c
> +++ b/net/can/af_can.c
> @@ -89,13 +89,7 @@ static atomic_t skbcounter = ATOMIC_INIT(0);
>  
>  int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>  {
> -	struct sock *sk = sock->sk;
> -
>  	switch (cmd) {
> -
> -	case SIOCGSTAMP:
> -		return sock_get_timestamp(sk, (struct timeval __user *)arg);
> -
>  	default:
>  		return -ENOIOCTLCMD;
>  	}
> diff --git a/net/can/bcm.c b/net/can/bcm.c
> index 79bb8afa9c0c..a34ee52f19ea 100644
> --- a/net/can/bcm.c
> +++ b/net/can/bcm.c
> @@ -1689,6 +1689,7 @@ static const struct proto_ops bcm_ops = {
>  	.getname       = sock_no_getname,
>  	.poll          = datagram_poll,
>  	.ioctl         = can_ioctl,	/* use can_ioctl() from af_can.c */
> +	.gettstamp     = sock_gettstamp,
>  	.listen        = sock_no_listen,
>  	.shutdown      = sock_no_shutdown,
>  	.setsockopt    = sock_no_setsockopt,
> diff --git a/net/can/raw.c b/net/can/raw.c
> index c70207537488..afcbff063a67 100644
> --- a/net/can/raw.c
> +++ b/net/can/raw.c
> @@ -846,6 +846,7 @@ static const struct proto_ops raw_ops = {
>  	.getname       = raw_getname,
>  	.poll          = datagram_poll,
>  	.ioctl         = can_ioctl,	/* use can_ioctl() from af_can.c */
> +	.gettstamp     = sock_gettstamp,
>  	.listen        = sock_no_listen,
>  	.shutdown      = sock_no_shutdown,
>  	.setsockopt    = raw_setsockopt,
> diff --git a/net/compat.c b/net/compat.c
> index eeea5eb71639..a031bd333092 100644
> --- a/net/compat.c
> +++ b/net/compat.c
> @@ -395,63 +395,6 @@ COMPAT_SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
>  	return __compat_sys_setsockopt(fd, level, optname, optval, optlen);
>  }
>  
> -int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
> -{
> -	struct compat_timeval __user *ctv;
> -	int err;
> -	struct timeval tv;
> -
> -	if (COMPAT_USE_64BIT_TIME)
> -		return sock_get_timestamp(sk, userstamp);
> -
> -	ctv = (struct compat_timeval __user *) userstamp;
> -	err = -ENOENT;
> -	sock_enable_timestamp(sk, SOCK_TIMESTAMP);
> -	tv = ktime_to_timeval(sock_read_timestamp(sk));
> -
> -	if (tv.tv_sec == -1)
> -		return err;
> -	if (tv.tv_sec == 0) {
> -		ktime_t kt = ktime_get_real();
> -		sock_write_timestamp(sk, kt);
> -		tv = ktime_to_timeval(kt);
> -	}
> -	err = 0;
> -	if (put_user(tv.tv_sec, &ctv->tv_sec) ||
> -			put_user(tv.tv_usec, &ctv->tv_usec))
> -		err = -EFAULT;
> -	return err;
> -}
> -EXPORT_SYMBOL(compat_sock_get_timestamp);
> -
> -int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
> -{
> -	struct compat_timespec __user *ctv;
> -	int err;
> -	struct timespec ts;
> -
> -	if (COMPAT_USE_64BIT_TIME)
> -		return sock_get_timestampns (sk, userstamp);
> -
> -	ctv = (struct compat_timespec __user *) userstamp;
> -	err = -ENOENT;
> -	sock_enable_timestamp(sk, SOCK_TIMESTAMP);
> -	ts = ktime_to_timespec(sock_read_timestamp(sk));
> -	if (ts.tv_sec == -1)
> -		return err;
> -	if (ts.tv_sec == 0) {
> -		ktime_t kt = ktime_get_real();
> -		sock_write_timestamp(sk, kt);
> -		ts = ktime_to_timespec(kt);
> -	}
> -	err = 0;
> -	if (put_user(ts.tv_sec, &ctv->tv_sec) ||
> -			put_user(ts.tv_nsec, &ctv->tv_nsec))
> -		err = -EFAULT;
> -	return err;
> -}
> -EXPORT_SYMBOL(compat_sock_get_timestampns);
> -
>  static int __compat_sys_getsockopt(int fd, int level, int optname,
>  				   char __user *optval,
>  				   int __user *optlen)
> diff --git a/net/core/sock.c b/net/core/sock.c
> index 782343bb925b..dc7c31d66200 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -2977,39 +2977,44 @@ bool lock_sock_fast(struct sock *sk)
>  }
>  EXPORT_SYMBOL(lock_sock_fast);
>  
> -int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
> +int sock_gettstamp(struct socket *sock, void __user *userstamp,
> +		   bool timeval, bool time32)
>  {
> -	struct timeval tv;
> +	struct sock *sk = sock->sk;
> +	struct timespec64 ts;
>  
>  	sock_enable_timestamp(sk, SOCK_TIMESTAMP);
> -	tv = ktime_to_timeval(sock_read_timestamp(sk));
> -	if (tv.tv_sec == -1)
> +	ts = ktime_to_timespec64(sk->sk_stamp);
> +	if (ts.tv_sec == -1)
>  		return -ENOENT;
> -	if (tv.tv_sec == 0) {
> +	if (ts.tv_sec == 0) {
>  		ktime_t kt = ktime_get_real();
> -		sock_write_timestamp(sk, kt);
> -		tv = ktime_to_timeval(kt);
> +		sock_write_timestamp(sk, kt);;
> +		ts = ktime_to_timespec64(kt);
>  	}
> -	return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
> -}
> -EXPORT_SYMBOL(sock_get_timestamp);
>  
> -int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
> -{
> -	struct timespec ts;
> +	if (timeval)
> +		ts.tv_nsec /= 1000;
>  
> -	sock_enable_timestamp(sk, SOCK_TIMESTAMP);
> -	ts = ktime_to_timespec(sock_read_timestamp(sk));
> -	if (ts.tv_sec == -1)
> -		return -ENOENT;
> -	if (ts.tv_sec == 0) {
> -		ktime_t kt = ktime_get_real();
> -		sock_write_timestamp(sk, kt);
> -		ts = ktime_to_timespec(sk->sk_stamp);
> +#ifdef CONFIG_COMPAT_32BIT_TIME
> +	if (time32)
> +		return put_old_timespec32(&ts, userstamp);
> +#endif
> +#ifdef CONFIG_SPARC64
> +	/* beware of padding in sparc64 timeval */
> +	if (timeval && !in_compat_syscall()) {
> +		struct __kernel_old_timeval __user tv = {
> +			.tv_sec = ts.tv_sec;
> +			.tv_usec = ts.tv_nsec;
> +		};
> +		if (copy_to_user(userstamp, &tv, sizeof(tv))
> +			return -EFAULT;
> +		return 0;
>  	}
> -	return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
> +#endif
> +	return put_timespec64(&ts, userstamp);
>  }
> -EXPORT_SYMBOL(sock_get_timestampns);
> +EXPORT_SYMBOL(sock_gettstamp);
>  
>  void sock_enable_timestamp(struct sock *sk, int flag)
>  {
> diff --git a/net/dccp/ipv4.c b/net/dccp/ipv4.c
> index 26a21d97b6b0..004535e4c070 100644
> --- a/net/dccp/ipv4.c
> +++ b/net/dccp/ipv4.c
> @@ -991,6 +991,7 @@ static const struct proto_ops inet_dccp_ops = {
>  	/* FIXME: work on tcp_poll to rename it to inet_csk_poll */
>  	.poll		   = dccp_poll,
>  	.ioctl		   = inet_ioctl,
> +	.gettstamp	   = sock_gettstamp,
>  	/* FIXME: work on inet_listen to rename it to sock_common_listen */
>  	.listen		   = inet_dccp_listen,
>  	.shutdown	   = inet_shutdown,
> diff --git a/net/dccp/ipv6.c b/net/dccp/ipv6.c
> index 57d84e9b7b6f..c4e4d1301062 100644
> --- a/net/dccp/ipv6.c
> +++ b/net/dccp/ipv6.c
> @@ -1075,6 +1075,7 @@ static const struct proto_ops inet6_dccp_ops = {
>  	.getname	   = inet6_getname,
>  	.poll		   = dccp_poll,
>  	.ioctl		   = inet6_ioctl,
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = inet_dccp_listen,
>  	.shutdown	   = inet_shutdown,
>  	.setsockopt	   = sock_common_setsockopt,
> diff --git a/net/ieee802154/socket.c b/net/ieee802154/socket.c
> index bc6b912603f1..ce2dfb997537 100644
> --- a/net/ieee802154/socket.c
> +++ b/net/ieee802154/socket.c
> @@ -164,10 +164,6 @@ static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
>  	struct sock *sk = sock->sk;
>  
>  	switch (cmd) {
> -	case SIOCGSTAMP:
> -		return sock_get_timestamp(sk, (struct timeval __user *)arg);
> -	case SIOCGSTAMPNS:
> -		return sock_get_timestampns(sk, (struct timespec __user *)arg);
>  	case SIOCGIFADDR:
>  	case SIOCSIFADDR:
>  		return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
> @@ -426,6 +422,7 @@ static const struct proto_ops ieee802154_raw_ops = {
>  	.getname	   = sock_no_getname,
>  	.poll		   = datagram_poll,
>  	.ioctl		   = ieee802154_sock_ioctl,
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = sock_no_listen,
>  	.shutdown	   = sock_no_shutdown,
>  	.setsockopt	   = sock_common_setsockopt,
> @@ -988,6 +985,7 @@ static const struct proto_ops ieee802154_dgram_ops = {
>  	.getname	   = sock_no_getname,
>  	.poll		   = datagram_poll,
>  	.ioctl		   = ieee802154_sock_ioctl,
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = sock_no_listen,
>  	.shutdown	   = sock_no_shutdown,
>  	.setsockopt	   = sock_common_setsockopt,
> diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
> index eab3ebde981e..0f46d87715aa 100644
> --- a/net/ipv4/af_inet.c
> +++ b/net/ipv4/af_inet.c
> @@ -911,12 +911,6 @@ int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>  	struct rtentry rt;
>  
>  	switch (cmd) {
> -	case SIOCGSTAMP:
> -		err = sock_get_timestamp(sk, (struct timeval __user *)arg);
> -		break;
> -	case SIOCGSTAMPNS:
> -		err = sock_get_timestampns(sk, (struct timespec __user *)arg);
> -		break;
>  	case SIOCADDRT:
>  	case SIOCDELRT:
>  		if (copy_from_user(&rt, p, sizeof(struct rtentry)))
> @@ -988,6 +982,7 @@ const struct proto_ops inet_stream_ops = {
>  	.getname	   = inet_getname,
>  	.poll		   = tcp_poll,
>  	.ioctl		   = inet_ioctl,
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = inet_listen,
>  	.shutdown	   = inet_shutdown,
>  	.setsockopt	   = sock_common_setsockopt,
> @@ -1023,6 +1018,7 @@ const struct proto_ops inet_dgram_ops = {
>  	.getname	   = inet_getname,
>  	.poll		   = udp_poll,
>  	.ioctl		   = inet_ioctl,
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = sock_no_listen,
>  	.shutdown	   = inet_shutdown,
>  	.setsockopt	   = sock_common_setsockopt,
> @@ -1055,6 +1051,7 @@ static const struct proto_ops inet_sockraw_ops = {
>  	.getname	   = inet_getname,
>  	.poll		   = datagram_poll,
>  	.ioctl		   = inet_ioctl,
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = sock_no_listen,
>  	.shutdown	   = inet_shutdown,
>  	.setsockopt	   = sock_common_setsockopt,
> diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
> index 2f45d2a3e3a3..6d1133fd89f6 100644
> --- a/net/ipv6/af_inet6.c
> +++ b/net/ipv6/af_inet6.c
> @@ -546,12 +546,6 @@ int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>  	struct net *net = sock_net(sk);
>  
>  	switch (cmd) {
> -	case SIOCGSTAMP:
> -		return sock_get_timestamp(sk, (struct timeval __user *)arg);
> -
> -	case SIOCGSTAMPNS:
> -		return sock_get_timestampns(sk, (struct timespec __user *)arg);
> -
>  	case SIOCADDRT:
>  	case SIOCDELRT:
>  
> @@ -584,6 +578,7 @@ const struct proto_ops inet6_stream_ops = {
>  	.getname	   = inet6_getname,
>  	.poll		   = tcp_poll,			/* ok		*/
>  	.ioctl		   = inet6_ioctl,		/* must change  */
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = inet_listen,		/* ok		*/
>  	.shutdown	   = inet_shutdown,		/* ok		*/
>  	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
> @@ -617,6 +612,7 @@ const struct proto_ops inet6_dgram_ops = {
>  	.getname	   = inet6_getname,
>  	.poll		   = udp_poll,			/* ok		*/
>  	.ioctl		   = inet6_ioctl,		/* must change  */
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = sock_no_listen,		/* ok		*/
>  	.shutdown	   = inet_shutdown,		/* ok		*/
>  	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
> diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c
> index 5a426226c762..84dbe21b71e5 100644
> --- a/net/ipv6/raw.c
> +++ b/net/ipv6/raw.c
> @@ -1356,6 +1356,7 @@ const struct proto_ops inet6_sockraw_ops = {
>  	.getname	   = inet6_getname,
>  	.poll		   = datagram_poll,		/* ok		*/
>  	.ioctl		   = inet6_ioctl,		/* must change  */
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = sock_no_listen,		/* ok		*/
>  	.shutdown	   = inet_shutdown,		/* ok		*/
>  	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
> diff --git a/net/l2tp/l2tp_ip.c b/net/l2tp/l2tp_ip.c
> index d4c60523c549..2cac910c1cd4 100644
> --- a/net/l2tp/l2tp_ip.c
> +++ b/net/l2tp/l2tp_ip.c
> @@ -618,6 +618,7 @@ static const struct proto_ops l2tp_ip_ops = {
>  	.getname	   = l2tp_ip_getname,
>  	.poll		   = datagram_poll,
>  	.ioctl		   = inet_ioctl,
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = sock_no_listen,
>  	.shutdown	   = inet_shutdown,
>  	.setsockopt	   = sock_common_setsockopt,
> diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c
> index 37a69df17cab..4ec546cc1dd6 100644
> --- a/net/l2tp/l2tp_ip6.c
> +++ b/net/l2tp/l2tp_ip6.c
> @@ -752,6 +752,7 @@ static const struct proto_ops l2tp_ip6_ops = {
>  	.getname	   = l2tp_ip6_getname,
>  	.poll		   = datagram_poll,
>  	.ioctl		   = inet6_ioctl,
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = sock_no_listen,
>  	.shutdown	   = inet_shutdown,
>  	.setsockopt	   = sock_common_setsockopt,
> diff --git a/net/netrom/af_netrom.c b/net/netrom/af_netrom.c
> index 1d3144d19903..4e97bbbf1916 100644
> --- a/net/netrom/af_netrom.c
> +++ b/net/netrom/af_netrom.c
> @@ -1199,7 +1199,6 @@ static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>  {
>  	struct sock *sk = sock->sk;
>  	void __user *argp = (void __user *)arg;
> -	int ret;
>  
>  	switch (cmd) {
>  	case TIOCOUTQ: {
> @@ -1225,18 +1224,6 @@ static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>  		return put_user(amount, (int __user *)argp);
>  	}
>  
> -	case SIOCGSTAMP:
> -		lock_sock(sk);
> -		ret = sock_get_timestamp(sk, argp);
> -		release_sock(sk);
> -		return ret;
> -
> -	case SIOCGSTAMPNS:
> -		lock_sock(sk);
> -		ret = sock_get_timestampns(sk, argp);
> -		release_sock(sk);
> -		return ret;
> -
>  	case SIOCGIFADDR:
>  	case SIOCSIFADDR:
>  	case SIOCGIFDSTADDR:
> @@ -1362,6 +1349,7 @@ static const struct proto_ops nr_proto_ops = {
>  	.getname	=	nr_getname,
>  	.poll		=	datagram_poll,
>  	.ioctl		=	nr_ioctl,
> +	.gettstamp	=	sock_gettstamp,
>  	.listen		=	nr_listen,
>  	.shutdown	=	sock_no_shutdown,
>  	.setsockopt	=	nr_setsockopt,
> diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
> index 9419c5cf4de5..b012dc5ffb6e 100644
> --- a/net/packet/af_packet.c
> +++ b/net/packet/af_packet.c
> @@ -4077,11 +4077,6 @@ static int packet_ioctl(struct socket *sock, unsigned int cmd,
>  		spin_unlock_bh(&sk->sk_receive_queue.lock);
>  		return put_user(amount, (int __user *)arg);
>  	}
> -	case SIOCGSTAMP:
> -		return sock_get_timestamp(sk, (struct timeval __user *)arg);
> -	case SIOCGSTAMPNS:
> -		return sock_get_timestampns(sk, (struct timespec __user *)arg);
> -
>  #ifdef CONFIG_INET
>  	case SIOCADDRT:
>  	case SIOCDELRT:
> @@ -4457,6 +4452,7 @@ static const struct proto_ops packet_ops_spkt = {
>  	.getname =	packet_getname_spkt,
>  	.poll =		datagram_poll,
>  	.ioctl =	packet_ioctl,
> +	.gettstamp =	sock_gettstamp,
>  	.listen =	sock_no_listen,
>  	.shutdown =	sock_no_shutdown,
>  	.setsockopt =	sock_no_setsockopt,
> @@ -4478,6 +4474,7 @@ static const struct proto_ops packet_ops = {
>  	.getname =	packet_getname,
>  	.poll =		packet_poll,
>  	.ioctl =	packet_ioctl,
> +	.gettstamp =	sock_gettstamp,
>  	.listen =	sock_no_listen,
>  	.shutdown =	sock_no_shutdown,
>  	.setsockopt =	packet_setsockopt,
> diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
> index b37e6e0a1026..7c5e8292cc0a 100644
> --- a/net/qrtr/qrtr.c
> +++ b/net/qrtr/qrtr.c
> @@ -968,9 +968,6 @@ static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>  			break;
>  		}
>  		break;
> -	case SIOCGSTAMP:
> -		rc = sock_get_timestamp(sk, argp);
> -		break;
>  	case SIOCADDRT:
>  	case SIOCDELRT:
>  	case SIOCSIFADDR:
> @@ -1033,6 +1030,7 @@ static const struct proto_ops qrtr_proto_ops = {
>  	.recvmsg	= qrtr_recvmsg,
>  	.getname	= qrtr_getname,
>  	.ioctl		= qrtr_ioctl,
> +	.gettstamp	= sock_gettstamp,
>  	.poll		= datagram_poll,
>  	.shutdown	= sock_no_shutdown,
>  	.setsockopt	= sock_no_setsockopt,
> diff --git a/net/rose/af_rose.c b/net/rose/af_rose.c
> index c96f63ffe31e..e274bc6e1458 100644
> --- a/net/rose/af_rose.c
> +++ b/net/rose/af_rose.c
> @@ -1301,12 +1301,6 @@ static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>  		return put_user(amount, (unsigned int __user *) argp);
>  	}
>  
> -	case SIOCGSTAMP:
> -		return sock_get_timestamp(sk, (struct timeval __user *) argp);
> -
> -	case SIOCGSTAMPNS:
> -		return sock_get_timestampns(sk, (struct timespec __user *) argp);
> -
>  	case SIOCGIFADDR:
>  	case SIOCSIFADDR:
>  	case SIOCGIFDSTADDR:
> @@ -1474,6 +1468,7 @@ static const struct proto_ops rose_proto_ops = {
>  	.getname	=	rose_getname,
>  	.poll		=	datagram_poll,
>  	.ioctl		=	rose_ioctl,
> +	.gettstamp	=	sock_gettstamp,
>  	.listen		=	rose_listen,
>  	.shutdown	=	sock_no_shutdown,
>  	.setsockopt	=	rose_setsockopt,
> diff --git a/net/sctp/ipv6.c b/net/sctp/ipv6.c
> index 6200cd2b4b99..188c47eb206e 100644
> --- a/net/sctp/ipv6.c
> +++ b/net/sctp/ipv6.c
> @@ -1030,6 +1030,7 @@ static const struct proto_ops inet6_seqpacket_ops = {
>  	.getname	   = sctp_getname,
>  	.poll		   = sctp_poll,
>  	.ioctl		   = inet6_ioctl,
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = sctp_inet_listen,
>  	.shutdown	   = inet_shutdown,
>  	.setsockopt	   = sock_common_setsockopt,
> diff --git a/net/sctp/protocol.c b/net/sctp/protocol.c
> index 951afdeea5e9..f0631bf486b6 100644
> --- a/net/sctp/protocol.c
> +++ b/net/sctp/protocol.c
> @@ -1026,6 +1026,7 @@ static const struct proto_ops inet_seqpacket_ops = {
>  	.getname	   = inet_getname,	/* Semantics are different.  */
>  	.poll		   = sctp_poll,
>  	.ioctl		   = inet_ioctl,
> +	.gettstamp	   = sock_gettstamp,
>  	.listen		   = sctp_inet_listen,
>  	.shutdown	   = inet_shutdown,	/* Looks harmless.  */
>  	.setsockopt	   = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
> diff --git a/net/socket.c b/net/socket.c
> index 8255f5bda0aa..ab624d42ead5 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -1164,6 +1164,15 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
>  
>  			err = open_related_ns(&net->ns, get_net_ns);
>  			break;
> +		case SIOCGSTAMP:
> +		case SIOCGSTAMPNS:
> +			if (!sock->ops->gettstamp) {
> +				err = -ENOIOCTLCMD;
> +				break;
> +			}
> +			err = sock->ops->gettstamp(sock, argp,
> +						   cmd == SIOCGSTAMP, false);
> +			break;
>  		default:
>  			err = sock_do_ioctl(net, sock, cmd, arg);
>  			break;
> @@ -2916,38 +2925,6 @@ void socket_seq_show(struct seq_file *seq)
>  #endif				/* CONFIG_PROC_FS */
>  
>  #ifdef CONFIG_COMPAT
> -static int do_siocgstamp(struct net *net, struct socket *sock,
> -			 unsigned int cmd, void __user *up)
> -{
> -	mm_segment_t old_fs = get_fs();
> -	struct timeval ktv;
> -	int err;
> -
> -	set_fs(KERNEL_DS);
> -	err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
> -	set_fs(old_fs);
> -	if (!err)
> -		err = compat_put_timeval(&ktv, up);
> -
> -	return err;
> -}
> -
> -static int do_siocgstampns(struct net *net, struct socket *sock,
> -			   unsigned int cmd, void __user *up)
> -{
> -	mm_segment_t old_fs = get_fs();
> -	struct timespec kts;
> -	int err;
> -
> -	set_fs(KERNEL_DS);
> -	err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
> -	set_fs(old_fs);
> -	if (!err)
> -		err = compat_put_timespec(&kts, up);
> -
> -	return err;
> -}
> -
>  static int compat_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
>  {
>  	struct compat_ifconf ifc32;
> @@ -3348,9 +3325,12 @@ static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
>  	case SIOCDELRT:
>  		return routing_ioctl(net, sock, cmd, argp);
>  	case SIOCGSTAMP:
> -		return do_siocgstamp(net, sock, cmd, argp);
>  	case SIOCGSTAMPNS:
> -		return do_siocgstampns(net, sock, cmd, argp);
> +		if (!sock->ops->gettstamp)
> +			return -ENOIOCTLCMD;
> +		return sock->ops->gettstamp(sock, argp, cmd == SIOCGSTAMP,
> +					    !COMPAT_USE_64BIT_TIME);
> +
>  	case SIOCBONDSLAVEINFOQUERY:
>  	case SIOCBONDINFOQUERY:
>  	case SIOCSHWTSTAMP:
> diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
> index 20a511398389..0ea48a52ce79 100644
> --- a/net/x25/af_x25.c
> +++ b/net/x25/af_x25.c
> @@ -1398,18 +1398,6 @@ static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>  		break;
>  	}
>  
> -	case SIOCGSTAMP:
> -		rc = -EINVAL;
> -		if (sk)
> -			rc = sock_get_timestamp(sk,
> -						(struct timeval __user *)argp);
> -		break;
> -	case SIOCGSTAMPNS:
> -		rc = -EINVAL;
> -		if (sk)
> -			rc = sock_get_timestampns(sk,
> -					(struct timespec __user *)argp);
> -		break;
>  	case SIOCGIFADDR:
>  	case SIOCSIFADDR:
>  	case SIOCGIFDSTADDR:
> @@ -1681,8 +1669,6 @@ static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
>  				unsigned long arg)
>  {
>  	void __user *argp = compat_ptr(arg);
> -	struct sock *sk = sock->sk;
> -
>  	int rc = -ENOIOCTLCMD;
>  
>  	switch(cmd) {
> @@ -1690,18 +1676,6 @@ static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
>  	case TIOCINQ:
>  		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
>  		break;
> -	case SIOCGSTAMP:
> -		rc = -EINVAL;
> -		if (sk)
> -			rc = compat_sock_get_timestamp(sk,
> -					(struct timeval __user*)argp);
> -		break;
> -	case SIOCGSTAMPNS:
> -		rc = -EINVAL;
> -		if (sk)
> -			rc = compat_sock_get_timestampns(sk,
> -					(struct timespec __user*)argp);
> -		break;
>  	case SIOCGIFADDR:
>  	case SIOCSIFADDR:
>  	case SIOCGIFDSTADDR:
> @@ -1765,6 +1739,7 @@ static const struct proto_ops x25_proto_ops = {
>  #ifdef CONFIG_COMPAT
>  	.compat_ioctl = compat_x25_ioctl,
>  #endif
> +	.gettstamp =	sock_gettstamp,
>  	.listen =	x25_listen,
>  	.shutdown =	sock_no_shutdown,
>  	.setsockopt =	x25_setsockopt,
> -- 
> 2.20.0
> 
> 
For the sctp bits
Acked-by: Neil Horman <nhorman@tuxdriver.com>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2019-04-16 20:32 [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling Arnd Bergmann
  2019-04-17  9:35 ` Neil Horman
@ 2019-04-17  9:59 ` Marc Kleine-Budde
  2019-04-17 14:46 ` Willem de Bruijn
  2 siblings, 0 replies; 16+ messages in thread
From: Marc Kleine-Budde @ 2019-04-17  9:59 UTC (permalink / raw)
  To: Arnd Bergmann, David S. Miller, Ralf Baechle, Marcel Holtmann,
	Johan Hedberg, Oliver Hartkopp, Gerrit Renker, Alexander Aring,
	Stefan Schmidt, Alexey Kuznetsov, Hideaki YOSHIFUJI,
	Vlad Yasevich, Neil Horman, Marcelo Ricardo Leitner,
	Andrew Hendry, Eric Dumazet, Willem de Bruijn, Deepa Dinamani
  Cc: netdev, linux-kernel, linux-hams, linux-bluetooth, linux-can,
	dccp, linux-wpan, linux-sctp, linux-x25


[-- Attachment #1.1: Type: text/plain, Size: 1449 bytes --]

On 4/16/19 10:32 PM, Arnd Bergmann wrote:
> The SIOCGSTAMP/SIOCGSTAMPNS ioctl commands are implemented by many
> socket protocol handlers, and all of those end up calling the same
> sock_get_timestamp()/sock_get_timestampns() helper functions, which
> results in a lot of duplicate code.
> 
> With the introduction of 64-bit time_t on 32-bit architectures, this
> gets worse, as we then need four different ioctl commands in each
> socket protocol implementation.
> 
> To simplify that, let's add a new .gettstamp() operation in
> struct proto_ops, and move ioctl implementation into the common
> sock_ioctl()/compat_sock_ioctl_trans() functions that these all go
> through.
> 
> We can reuse the sock_get_timestamp() implementation, but generalize
> it so it can deal with both native and compat mode, as well as
> timeval and timespec structures.
> 
> Acked-by: Stefan Schmidt <stefan@datenfreihafen.org>
> Link: https://lore.kernel.org/lkml/CAK8P3a038aDQQotzua_QtKGhq8O9n+rdiz2=WDCp82ys8eUT+A@mail.gmail.com/
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>

For the CAN part:

Acked-by: Marc Kleine-Budde <mkl@pengutronix.de>

regards,
Marc

-- 
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2019-04-16 20:32 [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling Arnd Bergmann
  2019-04-17  9:35 ` Neil Horman
  2019-04-17  9:59 ` Marc Kleine-Budde
@ 2019-04-17 14:46 ` Willem de Bruijn
  2019-04-17 16:19   ` Arnd Bergmann
  2 siblings, 1 reply; 16+ messages in thread
From: Willem de Bruijn @ 2019-04-17 14:46 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: David S. Miller, Ralf Baechle, Marcel Holtmann, Johan Hedberg,
	Oliver Hartkopp, Marc Kleine-Budde, Gerrit Renker,
	Alexander Aring, Stefan Schmidt, Alexey Kuznetsov,
	Hideaki YOSHIFUJI, Vlad Yasevich, Neil Horman,
	Marcelo Ricardo Leitner, Andrew Hendry, Eric Dumazet,
	Willem de Bruijn, Deepa Dinamani, Network Development, LKML,
	linux-hams, linux-bluetooth, linux-can, dccp, linux-wpan,
	linux-sctp, linux-x25

On Tue, Apr 16, 2019 at 4:38 PM Arnd Bergmann <arnd@arndb.de> wrote:
>
> The SIOCGSTAMP/SIOCGSTAMPNS ioctl commands are implemented by many
> socket protocol handlers, and all of those end up calling the same
> sock_get_timestamp()/sock_get_timestampns() helper functions, which
> results in a lot of duplicate code.
>
> With the introduction of 64-bit time_t on 32-bit architectures, this
> gets worse, as we then need four different ioctl commands in each
> socket protocol implementation.
>
> To simplify that, let's add a new .gettstamp() operation in
> struct proto_ops, and move ioctl implementation into the common
> sock_ioctl()/compat_sock_ioctl_trans() functions that these all go
> through.
>
> We can reuse the sock_get_timestamp() implementation, but generalize
> it so it can deal with both native and compat mode, as well as
> timeval and timespec structures.
>
> Acked-by: Stefan Schmidt <stefan@datenfreihafen.org>
> Link: https://lore.kernel.org/lkml/CAK8P3a038aDQQotzua_QtKGhq8O9n+rdiz2=WDCp82ys8eUT+A@mail.gmail.com/
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
> v2: reworked to not break sparc64 support

 From the discussion of v1 I thought you planned to unconditionally
call sock_gettstamp() for all protocols, avoiding the need to plumb in
all these new callbacks?

That is more concise, though this closer to the existing behavior. So,
fine either way.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2019-04-17 14:46 ` Willem de Bruijn
@ 2019-04-17 16:19   ` Arnd Bergmann
  2019-04-17 18:16     ` Willem de Bruijn
  0 siblings, 1 reply; 16+ messages in thread
From: Arnd Bergmann @ 2019-04-17 16:19 UTC (permalink / raw)
  To: Willem de Bruijn
  Cc: David S. Miller, Ralf Baechle, Marcel Holtmann, Johan Hedberg,
	Oliver Hartkopp, Marc Kleine-Budde, Gerrit Renker,
	Alexander Aring, Stefan Schmidt, Alexey Kuznetsov,
	Hideaki YOSHIFUJI, Vlad Yasevich, Neil Horman,
	Marcelo Ricardo Leitner, Andrew Hendry, Eric Dumazet,
	Willem de Bruijn, Deepa Dinamani, Network Development, LKML,
	linux-hams, Bluez mailing list, linux-can, dccp, linux-wpan,
	linux-sctp, linux-x25

On Wed, Apr 17, 2019 at 4:46 PM Willem de Bruijn
<willemdebruijn.kernel@gmail.com> wrote:
> On Tue, Apr 16, 2019 at 4:38 PM Arnd Bergmann <arnd@arndb.de> wrote:
> > The SIOCGSTAMP/SIOCGSTAMPNS ioctl commands are implemented by many
> > socket protocol handlers, and all of those end up calling the same
> > sock_get_timestamp()/sock_get_timestampns() helper functions, which
> > results in a lot of duplicate code.
> >
> > With the introduction of 64-bit time_t on 32-bit architectures, this
> > gets worse, as we then need four different ioctl commands in each
> > socket protocol implementation.
> >
> > To simplify that, let's add a new .gettstamp() operation in
> > struct proto_ops, and move ioctl implementation into the common
> > sock_ioctl()/compat_sock_ioctl_trans() functions that these all go
> > through.
> >
> > We can reuse the sock_get_timestamp() implementation, but generalize
> > it so it can deal with both native and compat mode, as well as
> > timeval and timespec structures.
> >
> > Acked-by: Stefan Schmidt <stefan@datenfreihafen.org>
> > Link: https://lore.kernel.org/lkml/CAK8P3a038aDQQotzua_QtKGhq8O9n+rdiz2=WDCp82ys8eUT+A@mail.gmail.com/
> > Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> > ---
> > v2: reworked to not break sparc64 support
>
> From the discussion of v1 I thought you planned to unconditionally
> call sock_gettstamp() for all protocols, avoiding the need to plumb in
> all these new callbacks?
>
> That is more concise, though this closer to the existing behavior. So,
> fine either way.

Thanks for the reminder. I have definitely waited too long before revisiting
this series, and only had a vague recollection of that discussion but could
not find it in the logs (found it now, and the Link I quoted...).

I would prefer to get this series into the coming merge window, and
probably don't have time to rework it completely by then, so I hope
the current version is ok.

I also found your comment on lock_sock(), which could be easily
added inside of sock_gettstamp() if you think we should have that.
There is one more issue I just noticed (I dropped the necessary
sock_read_timestamp()), so I have to repost the series anyway
to fix that.

       Arnd

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2019-04-17  9:35 ` Neil Horman
@ 2019-04-17 17:21   ` David Miller
  2019-04-17 20:15     ` Neil Horman
  0 siblings, 1 reply; 16+ messages in thread
From: David Miller @ 2019-04-17 17:21 UTC (permalink / raw)
  To: nhorman
  Cc: arnd, ralf, marcel, johan.hedberg, socketcan, mkl, gerrit,
	alex.aring, stefan, kuznet, yoshfuji, vyasevich, marcelo.leitner,
	andrew.hendry, edumazet, willemb, deepa.kernel, netdev,
	linux-kernel, linux-hams, linux-bluetooth, linux-can, dccp,
	linux-wpan, linux-sctp, linux-x25

From: Neil Horman <nhorman@tuxdriver.com>
Date: Wed, 17 Apr 2019 05:35:23 -0400

> Acked-by: Neil Horman <nhorman@tuxdriver.com>

Neil, please quote appropriately in the future.

I have to scroll through all of those pages of quoted patch just to
see this one line.

Thank you.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2019-04-17 16:19   ` Arnd Bergmann
@ 2019-04-17 18:16     ` Willem de Bruijn
  0 siblings, 0 replies; 16+ messages in thread
From: Willem de Bruijn @ 2019-04-17 18:16 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: David S. Miller, Ralf Baechle, Marcel Holtmann, Johan Hedberg,
	Oliver Hartkopp, Marc Kleine-Budde, Gerrit Renker,
	Alexander Aring, Stefan Schmidt, Alexey Kuznetsov,
	Hideaki YOSHIFUJI, Vlad Yasevich, Neil Horman,
	Marcelo Ricardo Leitner, Andrew Hendry, Eric Dumazet,
	Willem de Bruijn, Deepa Dinamani, Network Development, LKML,
	linux-hams, Bluez mailing list, linux-can, dccp, linux-wpan,
	linux-sctp, linux-x25

On Wed, Apr 17, 2019 at 12:19 PM Arnd Bergmann <arnd@arndb.de> wrote:
>
> On Wed, Apr 17, 2019 at 4:46 PM Willem de Bruijn
> <willemdebruijn.kernel@gmail.com> wrote:
> > On Tue, Apr 16, 2019 at 4:38 PM Arnd Bergmann <arnd@arndb.de> wrote:
> > > The SIOCGSTAMP/SIOCGSTAMPNS ioctl commands are implemented by many
> > > socket protocol handlers, and all of those end up calling the same
> > > sock_get_timestamp()/sock_get_timestampns() helper functions, which
> > > results in a lot of duplicate code.
> > >
> > > With the introduction of 64-bit time_t on 32-bit architectures, this
> > > gets worse, as we then need four different ioctl commands in each
> > > socket protocol implementation.
> > >
> > > To simplify that, let's add a new .gettstamp() operation in
> > > struct proto_ops, and move ioctl implementation into the common
> > > sock_ioctl()/compat_sock_ioctl_trans() functions that these all go
> > > through.
> > >
> > > We can reuse the sock_get_timestamp() implementation, but generalize
> > > it so it can deal with both native and compat mode, as well as
> > > timeval and timespec structures.
> > >
> > > Acked-by: Stefan Schmidt <stefan@datenfreihafen.org>
> > > Link: https://lore.kernel.org/lkml/CAK8P3a038aDQQotzua_QtKGhq8O9n+rdiz2=WDCp82ys8eUT+A@mail.gmail.com/
> > > Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> > > ---
> > > v2: reworked to not break sparc64 support
> >
> > From the discussion of v1 I thought you planned to unconditionally
> > call sock_gettstamp() for all protocols, avoiding the need to plumb in
> > all these new callbacks?
> >
> > That is more concise, though this closer to the existing behavior. So,
> > fine either way.
>
> Thanks for the reminder. I have definitely waited too long before revisiting
> this series, and only had a vague recollection of that discussion but could
> not find it in the logs (found it now, and the Link I quoted...).
>
> I would prefer to get this series into the coming merge window, and
> probably don't have time to rework it completely by then, so I hope
> the current version is ok.

Absolutely. This is a great simplification either way.

> I also found your comment on lock_sock(), which could be easily
> added inside of sock_gettstamp() if you think we should have that.

To remind, the issue is that sock_enable_timestamp should update both
sk_flags and net_enable_timestamp as one atomic operation, by holding
the socket lock. The lock is held when called from a setsockopt path.
And from some ioctl() implementations. The syzkaller reproducer
triggered through inet_release, so the most widely used paths are
buggy today.

Since the current state is inconsistent already, we can defer the fix.
I expect that it is now safe to add a lock around this logic without
triggering lockdep issues in any of the numerous paths. But just in
case, it still seems safer to do that in a separate patch that we can
revert or rework independent from this, if that would prove necessary.

> There is one more issue I just noticed (I dropped the necessary
> sock_read_timestamp()), so I have to repost the series anyway
> to fix that.

Instead of reading sk->sk_stamp directly? Yes, makes sense.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2019-04-17 17:21   ` David Miller
@ 2019-04-17 20:15     ` Neil Horman
  0 siblings, 0 replies; 16+ messages in thread
From: Neil Horman @ 2019-04-17 20:15 UTC (permalink / raw)
  To: David Miller
  Cc: arnd, ralf, marcel, johan.hedberg, socketcan, mkl, gerrit,
	alex.aring, stefan, kuznet, yoshfuji, vyasevich, marcelo.leitner,
	andrew.hendry, edumazet, willemb, deepa.kernel, netdev,
	linux-kernel, linux-hams, linux-bluetooth, linux-can, dccp,
	linux-wpan, linux-sctp, linux-x25

On Wed, Apr 17, 2019 at 10:21:12AM -0700, David Miller wrote:
> From: Neil Horman <nhorman@tuxdriver.com>
> Date: Wed, 17 Apr 2019 05:35:23 -0400
> 
> > Acked-by: Neil Horman <nhorman@tuxdriver.com>
> 
> Neil, please quote appropriately in the future.
> 
> I have to scroll through all of those pages of quoted patch just to
> see this one line.
> 
> Thank you.
> 
Sorry, completely forgot to snip.  Will remember in the future
Neil

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2018-08-29 12:59 Arnd Bergmann
  2018-08-30 20:09 ` Willem de Bruijn
  2018-09-13 12:28 ` Arnd Bergmann
@ 2018-09-21  9:14 ` Stefan Schmidt
  2 siblings, 0 replies; 16+ messages in thread
From: Stefan Schmidt @ 2018-09-21  9:14 UTC (permalink / raw)
  To: Arnd Bergmann, netdev, David S . Miller
  Cc: linux-arch, y2038, Eric Dumazet, Willem de Bruijn, linux-kernel,
	linux-hams, linux-bluetooth, linux-can, dccp, linux-wpan,
	linux-sctp, linux-x25

Hello Arnd.

On 8/29/18 2:59 PM, Arnd Bergmann wrote:
> The SIOCGSTAMP/SIOCGSTAMPNS ioctl commands are implemented by many
> socket protocol handlers, and all of those end up calling the same
> sock_get_timestamp()/sock_get_timestampns() helper functions, which
> results in a lot of duplicate code.
> 
> With the introduction of 64-bit time_t on 32-bit architectures, this
> gets worse, as we then need four different ioctl commands in each
> socket protocol implementation.
> 
> To simplify that, let's add a new .gettstamp() operation in
> struct proto_ops, and move ioctl implementation into the common
> sock_ioctl()/compat_sock_ioctl_trans() functions that these all go
> through.
> 
> We can reuse the sock_get_timestamp() implementation, but generalize
> it so it can deal with both native and compat mode, as well as
> timeval and timespec structures.
> 
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  include/linux/net.h          |  2 ++
>  include/net/compat.h         |  3 --
>  include/net/sock.h           |  4 +--
>  net/appletalk/ddp.c          |  7 +----
>  net/atm/ioctl.c              | 16 -----------
>  net/atm/pvc.c                |  1 +
>  net/atm/svc.c                |  1 +
>  net/ax25/af_ax25.c           |  9 +-----
>  net/bluetooth/af_bluetooth.c |  8 ------
>  net/bluetooth/l2cap_sock.c   |  1 +
>  net/bluetooth/rfcomm/sock.c  |  1 +
>  net/bluetooth/sco.c          |  1 +
>  net/can/af_can.c             |  6 ----
>  net/can/bcm.c                |  1 +
>  net/can/raw.c                |  1 +
>  net/compat.c                 | 54 ------------------------------------
>  net/core/sock.c              | 38 +++++++++++--------------
>  net/dccp/ipv4.c              |  1 +
>  net/dccp/ipv6.c              |  1 +
>  net/ieee802154/socket.c      |  6 ++--
>  net/ipv4/af_inet.c           |  9 ++----
>  net/ipv6/af_inet6.c          |  8 ++----
>  net/ipv6/raw.c               |  1 +
>  net/l2tp/l2tp_ip.c           |  1 +
>  net/l2tp/l2tp_ip6.c          |  1 +
>  net/netrom/af_netrom.c       | 14 +---------
>  net/packet/af_packet.c       |  7 ++---
>  net/qrtr/qrtr.c              |  4 +--
>  net/rose/af_rose.c           |  7 +----
>  net/sctp/ipv6.c              |  1 +
>  net/sctp/protocol.c          |  1 +
>  net/socket.c                 | 48 ++++++++++----------------------
>  net/x25/af_x25.c             | 27 +-----------------
>  33 files changed, 63 insertions(+), 228 deletions(-)

For the net/ieee802154/socket. part I am fine with this change.

Acked-by: Stefan Schmidt <stefan@datenfreihafen.org>

regards
Stefan Schmidt

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2018-08-29 12:59 Arnd Bergmann
  2018-08-30 20:09 ` Willem de Bruijn
@ 2018-09-13 12:28 ` Arnd Bergmann
  2018-09-21  9:14 ` Stefan Schmidt
  2 siblings, 0 replies; 16+ messages in thread
From: Arnd Bergmann @ 2018-09-13 12:28 UTC (permalink / raw)
  To: Networking, David Miller
  Cc: linux-arch, y2038 Mailman List, Eric Dumazet, Willem de Bruijn,
	Linux Kernel Mailing List, linux-hams, Bluez mailing list,
	linux-can, dccp, linux-wpan, linux-sctp, linux-x25

On Wed, Aug 29, 2018 at 3:03 PM Arnd Bergmann <arnd@arndb.de> wrote:

> diff --git a/net/core/sock.c b/net/core/sock.c
> index 3730eb855095..df17bbfaca27 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -2897,37 +2897,31 @@ bool lock_sock_fast(struct sock *sk)
>  }
>  EXPORT_SYMBOL(lock_sock_fast);
>
> -int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
> +int sock_gettstamp(struct socket *sock, void __user *userstamp,
> +                  bool timeval, bool time32)
>  {
> -       struct timeval tv;
> -
> -       sock_enable_timestamp(sk, SOCK_TIMESTAMP);
> -       tv = ktime_to_timeval(sk->sk_stamp);
> -       if (tv.tv_sec == -1)
> -               return -ENOENT;
> -       if (tv.tv_sec == 0) {
> -               sk->sk_stamp = ktime_get_real();
> -               tv = ktime_to_timeval(sk->sk_stamp);
> -       }
> -       return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
> -}
> -EXPORT_SYMBOL(sock_get_timestamp);

As  I just learned, sparc64 uses a 32-bit suseconds_t, so this
function always leaked 32 bits of kernel stack data by copying
the padding bytes of 'tv' into user space.

Linux-4.11 and higher could avoid that with
CONFIG_GCC_PLUGIN_STRUCTLEAK, but older kernels
have been affected since socket timestamps were first added.

The same thing is probably true of many other interfaces
that pass a timeval.

> -int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
> -{
> -       struct timespec ts;
> +       struct sock *sk = sock->sk;
> +       struct timespec64 ts;
>
>         sock_enable_timestamp(sk, SOCK_TIMESTAMP);
> -       ts = ktime_to_timespec(sk->sk_stamp);
> +       ts = ktime_to_timespec64(sk->sk_stamp);
>         if (ts.tv_sec == -1)
>                 return -ENOENT;
>         if (ts.tv_sec == 0) {
>                 sk->sk_stamp = ktime_get_real();
> -               ts = ktime_to_timespec(sk->sk_stamp);
> +               ts = ktime_to_timespec64(sk->sk_stamp);
>         }
> -       return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
> +
> +       if (timeval)
> +               ts.tv_nsec /= 1000;
> +#ifdef CONFIG_COMPAT_32BIT_TIME
> +       if (time32)
> +               return put_old_timespec32(&ts, userstamp);
> +#endif
> +
> +       return put_timespec64(&ts, userstamp);
>  }

My new implementation is worse here: it no longer leaks stack
data, but since we now write a big-endian 64-bit microseconds
value, the microseconds are in the wrong place and will
be interpreted as zero by user space...

I'll also have to revisit a few other similar patches I did for
y2038, to figure out what they should do on sparc64.

         Arnd

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2018-08-31 14:00       ` Arnd Bergmann
@ 2018-08-31 15:08         ` Willem de Bruijn
  0 siblings, 0 replies; 16+ messages in thread
From: Willem de Bruijn @ 2018-08-31 15:08 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Network Development, David Miller, linux-arch,
	y2038 Mailman List, Eric Dumazet, Willem de Bruijn, LKML,
	linux-hams, linux-bluetooth, linux-can, dccp, linux-wpan,
	linux-sctp, linux-x25

> > > Looking at it again, it seems that sock_gettstamp() should
> > > actually deal with this gracefully: it will return a -EINVAL
> > > error condition if the timestamp remains at the
> > > SK_DEFAULT_STAMP initial value, which is probably
> > > just as appropriate (or better) as the current -ENOTTY
> > > default, and if we are actually recording timestamps, we
> > > might just as well report them.
> >
> > Yes, that's a nice solution. There is always some risk in changing
> > error codes. But ioctl callers should be able to support newly
> > implemented functionality. Even if partially implemented and
> > returning ENOENT instead of ENOIOCTLCMD.
>
> Ok, so do you think we should stay with the current version
> for now, and change the two points later, or should I rework
> it to integrate the locking and removing the callback?
>
> I suppose the series actually gets nicer without the
> callback, since I can simply add the generic timestamping
> implementation first, and then remove the dead ioctl
> handlers.

Agreed. I would add the locks in a separate patch, if only on the
off-chance that lockdep discovers something and it will be easier
to bisect and revise independently. I can also follow up with that
patch outside this set, of course.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2018-08-31 13:37     ` Willem de Bruijn
@ 2018-08-31 14:00       ` Arnd Bergmann
  2018-08-31 15:08         ` Willem de Bruijn
  0 siblings, 1 reply; 16+ messages in thread
From: Arnd Bergmann @ 2018-08-31 14:00 UTC (permalink / raw)
  To: Willem de Bruijn
  Cc: Networking, David Miller, linux-arch, y2038 Mailman List,
	Eric Dumazet, Willem de Bruijn, Linux Kernel Mailing List,
	linux-hams, Bluez mailing list, linux-can, dccp, linux-wpan,
	linux-sctp, linux-x25

On Fri, Aug 31, 2018 at 3:38 PM Willem de Bruijn
<willemdebruijn.kernel@gmail.com> wrote:
> On Fri, Aug 31, 2018 at 6:31 AM Arnd Bergmann <arnd@arndb.de> wrote:
> > On Thu, Aug 30, 2018 at 10:10 PM Willem de Bruijn
> > <willemdebruijn.kernel@gmail.com> wrote:
> > > On Wed, Aug 29, 2018 at 9:05 AM Arnd Bergmann <arnd@arndb.de> wrote:
> > > If this is the only valid implementation of .gettstamp, the indirect
> > > call could be avoided in favor of a simple branch.
> >
> > I thought about that as well, but I could not come up with a
> > good way to encode the difference between socket protocols
> > that allow timestamping and those that don't.
> >
> > I think ideally we would just call sock_gettstamp() unconditonally
> > on every socket, and have that function decide whether timestamps
> > make sense or not. The part I did not understand is which ones
> > actually want the timestamps or not. Most protocols that
> > implement the ioctls also assign skb->tstamp, but there are some
> > protocols in which I could not see skb->tstamp ever being set,
> > and some that set it but don't seem to have the ioctls.
>
> These probably only use cmsgs SCM_TIMESTAMP(NS|IMG)
> to read timestamps.

Good point. FWIW, I have discussed with Deepa how those should
be modified for y2038, but we don't have any current patches for
those.

> > Looking at it again, it seems that sock_gettstamp() should
> > actually deal with this gracefully: it will return a -EINVAL
> > error condition if the timestamp remains at the
> > SK_DEFAULT_STAMP initial value, which is probably
> > just as appropriate (or better) as the current -ENOTTY
> > default, and if we are actually recording timestamps, we
> > might just as well report them.
>
> Yes, that's a nice solution. There is always some risk in changing
> error codes. But ioctl callers should be able to support newly
> implemented functionality. Even if partially implemented and
> returning ENOENT instead of ENOIOCTLCMD.

Ok, so do you think we should stay with the current version
for now, and change the two points later, or should I rework
it to integrate the locking and removing the callback?

I suppose the series actually gets nicer without the
callback, since I can simply add the generic timestamping
implementation first, and then remove the dead ioctl
handlers.

        Arnd

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2018-08-31 10:31   ` Arnd Bergmann
@ 2018-08-31 13:37     ` Willem de Bruijn
  2018-08-31 14:00       ` Arnd Bergmann
  0 siblings, 1 reply; 16+ messages in thread
From: Willem de Bruijn @ 2018-08-31 13:37 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Network Development, David Miller, linux-arch,
	y2038 Mailman List, Eric Dumazet, Willem de Bruijn, LKML,
	linux-hams, linux-bluetooth, linux-can, dccp, linux-wpan,
	linux-sctp, linux-x25

On Fri, Aug 31, 2018 at 6:31 AM Arnd Bergmann <arnd@arndb.de> wrote:
>
> On Thu, Aug 30, 2018 at 10:10 PM Willem de Bruijn
> <willemdebruijn.kernel@gmail.com> wrote:
> >
> > On Wed, Aug 29, 2018 at 9:05 AM Arnd Bergmann <arnd@arndb.de> wrote:
> > >
> > > The SIOCGSTAMP/SIOCGSTAMPNS ioctl commands are implemented by many
> > > socket protocol handlers, and all of those end up calling the same
> > > sock_get_timestamp()/sock_get_timestampns() helper functions, which
> > > results in a lot of duplicate code.
> > >
> > > With the introduction of 64-bit time_t on 32-bit architectures, this
> > > gets worse, as we then need four different ioctl commands in each
> > > socket protocol implementation.
> > >
> > > To simplify that, let's add a new .gettstamp() operation in
> > > struct proto_ops, and move ioctl implementation into the common
> > > sock_ioctl()/compat_sock_ioctl_trans() functions that these all go
> > > through.
> > >
> > > We can reuse the sock_get_timestamp() implementation, but generalize
> > > it so it can deal with both native and compat mode, as well as
> > > timeval and timespec structures.
> > >
> > > Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> >
> > This also will simplify fixing a recently reported race condition with
> > sock_get_timestamp [1]. That calls sock_enable_timestamp, which
> > modifies sk->sk_flags, without taking the socket lock. Currently some
> > callers of sock_get_timestamp hold the lock (ax25, netrom, qrtr), many
> > don't. See also how this patch removes the lock_sock in the netrom
> > case. Moving the call to sock_gettstamp outside the protocol handlers
> > will allow taking the lock inside the function.
>
> I suppose it would be best to always take that lock then, rather than
> removing the lock as my patch does at the moment.

Yes, although that can also be a separate follow-on patch. The
patch as is better matches the existing behavior of the majority
of protocols.

> > If this is the only valid implementation of .gettstamp, the indirect
> > call could be avoided in favor of a simple branch.
>
> I thought about that as well, but I could not come up with a
> good way to encode the difference between socket protocols
> that allow timestamping and those that don't.
>
> I think ideally we would just call sock_gettstamp() unconditonally
> on every socket, and have that function decide whether timestamps
> make sense or not. The part I did not understand is which ones
> actually want the timestamps or not. Most protocols that
> implement the ioctls also assign skb->tstamp, but there are some
> protocols in which I could not see skb->tstamp ever being set,
> and some that set it but don't seem to have the ioctls.

These probably only use cmsgs SCM_TIMESTAMP(NS|IMG)
to read timestamps.

> Looking at it again, it seems that sock_gettstamp() should
> actually deal with this gracefully: it will return a -EINVAL
> error condition if the timestamp remains at the
> SK_DEFAULT_STAMP initial value, which is probably
> just as appropriate (or better) as the current -ENOTTY
> default, and if we are actually recording timestamps, we
> might just as well report them.

Yes, that's a nice solution. There is always some risk in changing
error codes. But ioctl callers should be able to support newly
implemented functionality. Even if partially implemented and
returning ENOENT instead of ENOIOCTLCMD.

> > Acked-by: Willem de Bruijn <willemb@google.com>
>
> Thanks,
>
>       Arnd

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2018-08-30 20:09 ` Willem de Bruijn
@ 2018-08-31 10:31   ` Arnd Bergmann
  2018-08-31 13:37     ` Willem de Bruijn
  0 siblings, 1 reply; 16+ messages in thread
From: Arnd Bergmann @ 2018-08-31 10:31 UTC (permalink / raw)
  To: Willem de Bruijn
  Cc: Networking, David Miller, linux-arch, y2038 Mailman List,
	Eric Dumazet, Willem de Bruijn, Linux Kernel Mailing List,
	linux-hams, Bluez mailing list, linux-can, dccp, linux-wpan,
	linux-sctp, linux-x25

On Thu, Aug 30, 2018 at 10:10 PM Willem de Bruijn
<willemdebruijn.kernel@gmail.com> wrote:
>
> On Wed, Aug 29, 2018 at 9:05 AM Arnd Bergmann <arnd@arndb.de> wrote:
> >
> > The SIOCGSTAMP/SIOCGSTAMPNS ioctl commands are implemented by many
> > socket protocol handlers, and all of those end up calling the same
> > sock_get_timestamp()/sock_get_timestampns() helper functions, which
> > results in a lot of duplicate code.
> >
> > With the introduction of 64-bit time_t on 32-bit architectures, this
> > gets worse, as we then need four different ioctl commands in each
> > socket protocol implementation.
> >
> > To simplify that, let's add a new .gettstamp() operation in
> > struct proto_ops, and move ioctl implementation into the common
> > sock_ioctl()/compat_sock_ioctl_trans() functions that these all go
> > through.
> >
> > We can reuse the sock_get_timestamp() implementation, but generalize
> > it so it can deal with both native and compat mode, as well as
> > timeval and timespec structures.
> >
> > Signed-off-by: Arnd Bergmann <arnd@arndb.de>
>
> This also will simplify fixing a recently reported race condition with
> sock_get_timestamp [1]. That calls sock_enable_timestamp, which
> modifies sk->sk_flags, without taking the socket lock. Currently some
> callers of sock_get_timestamp hold the lock (ax25, netrom, qrtr), many
> don't. See also how this patch removes the lock_sock in the netrom
> case. Moving the call to sock_gettstamp outside the protocol handlers
> will allow taking the lock inside the function.

I suppose it would be best to always take that lock then, rather than
removing the lock as my patch does at the moment.

> If this is the only valid implementation of .gettstamp, the indirect
> call could be avoided in favor of a simple branch.

I thought about that as well, but I could not come up with a
good way to encode the difference between socket protocols
that allow timestamping and those that don't.

I think ideally we would just call sock_gettstamp() unconditonally
on every socket, and have that function decide whether timestamps
make sense or not. The part I did not understand is which ones
actually want the timestamps or not. Most protocols that
implement the ioctls also assign skb->tstamp, but there are some
protocols in which I could not see skb->tstamp ever being set,
and some that set it but don't seem to have the ioctls.

Looking at it again, it seems that sock_gettstamp() should
actually deal with this gracefully: it will return a -EINVAL
error condition if the timestamp remains at the
SK_DEFAULT_STAMP initial value, which is probably
just as appropriate (or better) as the current -ENOTTY
default, and if we are actually recording timestamps, we
might just as well report them.

> Acked-by: Willem de Bruijn <willemb@google.com>

Thanks,

      Arnd

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
  2018-08-29 12:59 Arnd Bergmann
@ 2018-08-30 20:09 ` Willem de Bruijn
  2018-08-31 10:31   ` Arnd Bergmann
  2018-09-13 12:28 ` Arnd Bergmann
  2018-09-21  9:14 ` Stefan Schmidt
  2 siblings, 1 reply; 16+ messages in thread
From: Willem de Bruijn @ 2018-08-30 20:09 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Network Development, David Miller, linux-arch,
	y2038 Mailman List, Eric Dumazet, Willem de Bruijn, LKML,
	linux-hams, linux-bluetooth, linux-can, dccp, linux-wpan,
	linux-sctp, linux-x25

On Wed, Aug 29, 2018 at 9:05 AM Arnd Bergmann <arnd@arndb.de> wrote:
>
> The SIOCGSTAMP/SIOCGSTAMPNS ioctl commands are implemented by many
> socket protocol handlers, and all of those end up calling the same
> sock_get_timestamp()/sock_get_timestampns() helper functions, which
> results in a lot of duplicate code.
>
> With the introduction of 64-bit time_t on 32-bit architectures, this
> gets worse, as we then need four different ioctl commands in each
> socket protocol implementation.
>
> To simplify that, let's add a new .gettstamp() operation in
> struct proto_ops, and move ioctl implementation into the common
> sock_ioctl()/compat_sock_ioctl_trans() functions that these all go
> through.
>
> We can reuse the sock_get_timestamp() implementation, but generalize
> it so it can deal with both native and compat mode, as well as
> timeval and timespec structures.
>
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>

This also will simplify fixing a recently reported race condition with
sock_get_timestamp [1]. That calls sock_enable_timestamp, which
modifies sk->sk_flags, without taking the socket lock. Currently some
callers of sock_get_timestamp hold the lock (ax25, netrom, qrtr), many
don't. See also how this patch removes the lock_sock in the netrom
case. Moving the call to sock_gettstamp outside the protocol handlers
will allow taking the lock inside the function.

If this is the only valid implementation of .gettstamp, the indirect
call could be avoided in favor of a simple branch.

Thanks,

Acked-by: Willem de Bruijn <willemb@google.com>

[1] http://lkml.kernel.org/r/20180518080308.GA28587@dragonet.kaist.ac.kr

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling
@ 2018-08-29 12:59 Arnd Bergmann
  2018-08-30 20:09 ` Willem de Bruijn
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Arnd Bergmann @ 2018-08-29 12:59 UTC (permalink / raw)
  To: netdev, David S . Miller
  Cc: linux-arch, y2038, Arnd Bergmann, Eric Dumazet, Willem de Bruijn,
	linux-kernel, linux-hams, linux-bluetooth, linux-can, dccp,
	linux-wpan, linux-sctp, linux-x25

The SIOCGSTAMP/SIOCGSTAMPNS ioctl commands are implemented by many
socket protocol handlers, and all of those end up calling the same
sock_get_timestamp()/sock_get_timestampns() helper functions, which
results in a lot of duplicate code.

With the introduction of 64-bit time_t on 32-bit architectures, this
gets worse, as we then need four different ioctl commands in each
socket protocol implementation.

To simplify that, let's add a new .gettstamp() operation in
struct proto_ops, and move ioctl implementation into the common
sock_ioctl()/compat_sock_ioctl_trans() functions that these all go
through.

We can reuse the sock_get_timestamp() implementation, but generalize
it so it can deal with both native and compat mode, as well as
timeval and timespec structures.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 include/linux/net.h          |  2 ++
 include/net/compat.h         |  3 --
 include/net/sock.h           |  4 +--
 net/appletalk/ddp.c          |  7 +----
 net/atm/ioctl.c              | 16 -----------
 net/atm/pvc.c                |  1 +
 net/atm/svc.c                |  1 +
 net/ax25/af_ax25.c           |  9 +-----
 net/bluetooth/af_bluetooth.c |  8 ------
 net/bluetooth/l2cap_sock.c   |  1 +
 net/bluetooth/rfcomm/sock.c  |  1 +
 net/bluetooth/sco.c          |  1 +
 net/can/af_can.c             |  6 ----
 net/can/bcm.c                |  1 +
 net/can/raw.c                |  1 +
 net/compat.c                 | 54 ------------------------------------
 net/core/sock.c              | 38 +++++++++++--------------
 net/dccp/ipv4.c              |  1 +
 net/dccp/ipv6.c              |  1 +
 net/ieee802154/socket.c      |  6 ++--
 net/ipv4/af_inet.c           |  9 ++----
 net/ipv6/af_inet6.c          |  8 ++----
 net/ipv6/raw.c               |  1 +
 net/l2tp/l2tp_ip.c           |  1 +
 net/l2tp/l2tp_ip6.c          |  1 +
 net/netrom/af_netrom.c       | 14 +---------
 net/packet/af_packet.c       |  7 ++---
 net/qrtr/qrtr.c              |  4 +--
 net/rose/af_rose.c           |  7 +----
 net/sctp/ipv6.c              |  1 +
 net/sctp/protocol.c          |  1 +
 net/socket.c                 | 48 ++++++++++----------------------
 net/x25/af_x25.c             | 27 +-----------------
 33 files changed, 63 insertions(+), 228 deletions(-)

diff --git a/include/linux/net.h b/include/linux/net.h
index e0930678c8bf..2be3e9c772fe 100644
--- a/include/linux/net.h
+++ b/include/linux/net.h
@@ -155,6 +155,8 @@ struct proto_ops {
 	int	 	(*compat_ioctl) (struct socket *sock, unsigned int cmd,
 				      unsigned long arg);
 #endif
+	int		(*gettstamp) (struct socket *sock, void __user *userstamp,
+				      bool timeval, bool time32);
 	int		(*listen)    (struct socket *sock, int len);
 	int		(*shutdown)  (struct socket *sock, int flags);
 	int		(*setsockopt)(struct socket *sock, int level,
diff --git a/include/net/compat.h b/include/net/compat.h
index 4c6d75612b6c..f277653c7e17 100644
--- a/include/net/compat.h
+++ b/include/net/compat.h
@@ -30,9 +30,6 @@ struct compat_cmsghdr {
 	compat_int_t	cmsg_type;
 };
 
-int compat_sock_get_timestamp(struct sock *, struct timeval __user *);
-int compat_sock_get_timestampns(struct sock *, struct timespec __user *);
-
 #else /* defined(CONFIG_COMPAT) */
 /*
  * To avoid compiler warnings:
diff --git a/include/net/sock.h b/include/net/sock.h
index 433f45fc2d68..ef6c9409dc75 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1584,6 +1584,8 @@ int sock_setsockopt(struct socket *sock, int level, int op,
 
 int sock_getsockopt(struct socket *sock, int level, int op,
 		    char __user *optval, int __user *optlen);
+int sock_gettstamp(struct socket *sock, void __user *userstamp,
+		   bool timeval, bool time32);
 struct sk_buff *sock_alloc_send_skb(struct sock *sk, unsigned long size,
 				    int noblock, int *errcode);
 struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
@@ -2423,8 +2425,6 @@ static inline bool sk_listener(const struct sock *sk)
 }
 
 void sock_enable_timestamp(struct sock *sk, int flag);
-int sock_get_timestamp(struct sock *, struct timeval __user *);
-int sock_get_timestampns(struct sock *, struct timespec __user *);
 int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len, int level,
 		       int type);
 
diff --git a/net/appletalk/ddp.c b/net/appletalk/ddp.c
index 9b6bc5abe946..a21a643997aa 100644
--- a/net/appletalk/ddp.c
+++ b/net/appletalk/ddp.c
@@ -1806,12 +1806,6 @@ static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		rc = put_user(amount, (int __user *)argp);
 		break;
 	}
-	case SIOCGSTAMP:
-		rc = sock_get_timestamp(sk, argp);
-		break;
-	case SIOCGSTAMPNS:
-		rc = sock_get_timestampns(sk, argp);
-		break;
 	/* Routing */
 	case SIOCADDRT:
 	case SIOCDELRT:
@@ -1871,6 +1865,7 @@ static const struct proto_ops atalk_dgram_ops = {
 	.getname	= atalk_getname,
 	.poll		= datagram_poll,
 	.ioctl		= atalk_ioctl,
+	.gettstamp	= sock_gettstamp,
 #ifdef CONFIG_COMPAT
 	.compat_ioctl	= atalk_compat_ioctl,
 #endif
diff --git a/net/atm/ioctl.c b/net/atm/ioctl.c
index 2ff0e5e470e3..d955b683aa7c 100644
--- a/net/atm/ioctl.c
+++ b/net/atm/ioctl.c
@@ -81,22 +81,6 @@ static int do_vcc_ioctl(struct socket *sock, unsigned int cmd,
 				 (int __user *)argp) ? -EFAULT : 0;
 		goto done;
 	}
-	case SIOCGSTAMP: /* borrowed from IP */
-#ifdef CONFIG_COMPAT
-		if (compat)
-			error = compat_sock_get_timestamp(sk, argp);
-		else
-#endif
-			error = sock_get_timestamp(sk, argp);
-		goto done;
-	case SIOCGSTAMPNS: /* borrowed from IP */
-#ifdef CONFIG_COMPAT
-		if (compat)
-			error = compat_sock_get_timestampns(sk, argp);
-		else
-#endif
-			error = sock_get_timestampns(sk, argp);
-		goto done;
 	case ATM_SETSC:
 		net_warn_ratelimited("ATM_SETSC is obsolete; used by %s:%d\n",
 				     current->comm, task_pid_nr(current));
diff --git a/net/atm/pvc.c b/net/atm/pvc.c
index 2cb10af16afc..02bd2a436bdf 100644
--- a/net/atm/pvc.c
+++ b/net/atm/pvc.c
@@ -118,6 +118,7 @@ static const struct proto_ops pvc_proto_ops = {
 #ifdef CONFIG_COMPAT
 	.compat_ioctl = vcc_compat_ioctl,
 #endif
+	.gettstamp =	sock_gettstamp,
 	.listen =	sock_no_listen,
 	.shutdown =	pvc_shutdown,
 	.setsockopt =	pvc_setsockopt,
diff --git a/net/atm/svc.c b/net/atm/svc.c
index 2f91b766ac42..908cbb8654f5 100644
--- a/net/atm/svc.c
+++ b/net/atm/svc.c
@@ -641,6 +641,7 @@ static const struct proto_ops svc_proto_ops = {
 #ifdef CONFIG_COMPAT
 	.compat_ioctl =	svc_compat_ioctl,
 #endif
+	.gettstamp =	sock_gettstamp,
 	.listen =	svc_listen,
 	.shutdown =	svc_shutdown,
 	.setsockopt =	svc_setsockopt,
diff --git a/net/ax25/af_ax25.c b/net/ax25/af_ax25.c
index c603d33d5410..121f7b877df9 100644
--- a/net/ax25/af_ax25.c
+++ b/net/ax25/af_ax25.c
@@ -1707,14 +1707,6 @@ static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		break;
 	}
 
-	case SIOCGSTAMP:
-		res = sock_get_timestamp(sk, argp);
-		break;
-
-	case SIOCGSTAMPNS:
-		res = sock_get_timestampns(sk, argp);
-		break;
-
 	case SIOCAX25ADDUID:	/* Add a uid to the uid/call map table */
 	case SIOCAX25DELUID:	/* Delete a uid from the uid/call map table */
 	case SIOCAX25GETUID: {
@@ -1943,6 +1935,7 @@ static const struct proto_ops ax25_proto_ops = {
 	.getname	= ax25_getname,
 	.poll		= datagram_poll,
 	.ioctl		= ax25_ioctl,
+	.gettstamp	= sock_gettstamp,
 	.listen		= ax25_listen,
 	.shutdown	= ax25_shutdown,
 	.setsockopt	= ax25_setsockopt,
diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
index deacc52d7ff1..34e15ca66779 100644
--- a/net/bluetooth/af_bluetooth.c
+++ b/net/bluetooth/af_bluetooth.c
@@ -511,14 +511,6 @@ int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		err = put_user(amount, (int __user *) arg);
 		break;
 
-	case SIOCGSTAMP:
-		err = sock_get_timestamp(sk, (struct timeval __user *) arg);
-		break;
-
-	case SIOCGSTAMPNS:
-		err = sock_get_timestampns(sk, (struct timespec __user *) arg);
-		break;
-
 	default:
 		err = -ENOIOCTLCMD;
 		break;
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index 686bdc6b35b0..e8fed07a49d5 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -1655,6 +1655,7 @@ static const struct proto_ops l2cap_sock_ops = {
 	.recvmsg	= l2cap_sock_recvmsg,
 	.poll		= bt_sock_poll,
 	.ioctl		= bt_sock_ioctl,
+	.gettstamp	= sock_gettstamp,
 	.mmap		= sock_no_mmap,
 	.socketpair	= sock_no_socketpair,
 	.shutdown	= l2cap_sock_shutdown,
diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index d606e9212291..a1dde6fab323 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -1049,6 +1049,7 @@ static const struct proto_ops rfcomm_sock_ops = {
 	.setsockopt	= rfcomm_sock_setsockopt,
 	.getsockopt	= rfcomm_sock_getsockopt,
 	.ioctl		= rfcomm_sock_ioctl,
+	.gettstamp	= sock_gettstamp,
 	.poll		= bt_sock_poll,
 	.socketpair	= sock_no_socketpair,
 	.mmap		= sock_no_mmap
diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c
index 8f0f9279eac9..e7e5b3ed0394 100644
--- a/net/bluetooth/sco.c
+++ b/net/bluetooth/sco.c
@@ -1200,6 +1200,7 @@ static const struct proto_ops sco_sock_ops = {
 	.recvmsg	= sco_sock_recvmsg,
 	.poll		= bt_sock_poll,
 	.ioctl		= bt_sock_ioctl,
+	.gettstamp	= sock_gettstamp,
 	.mmap		= sock_no_mmap,
 	.socketpair	= sock_no_socketpair,
 	.shutdown	= sco_sock_shutdown,
diff --git a/net/can/af_can.c b/net/can/af_can.c
index 1684ba5b51eb..e8fd5dc1780a 100644
--- a/net/can/af_can.c
+++ b/net/can/af_can.c
@@ -89,13 +89,7 @@ static atomic_t skbcounter = ATOMIC_INIT(0);
 
 int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 {
-	struct sock *sk = sock->sk;
-
 	switch (cmd) {
-
-	case SIOCGSTAMP:
-		return sock_get_timestamp(sk, (struct timeval __user *)arg);
-
 	default:
 		return -ENOIOCTLCMD;
 	}
diff --git a/net/can/bcm.c b/net/can/bcm.c
index 0af8f0db892a..db3e521b9f47 100644
--- a/net/can/bcm.c
+++ b/net/can/bcm.c
@@ -1662,6 +1662,7 @@ static const struct proto_ops bcm_ops = {
 	.getname       = sock_no_getname,
 	.poll          = datagram_poll,
 	.ioctl         = can_ioctl,	/* use can_ioctl() from af_can.c */
+	.gettstamp     = sock_gettstamp,
 	.listen        = sock_no_listen,
 	.shutdown      = sock_no_shutdown,
 	.setsockopt    = sock_no_setsockopt,
diff --git a/net/can/raw.c b/net/can/raw.c
index 1051eee82581..968f6f8082a1 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -845,6 +845,7 @@ static const struct proto_ops raw_ops = {
 	.getname       = raw_getname,
 	.poll          = datagram_poll,
 	.ioctl         = can_ioctl,	/* use can_ioctl() from af_can.c */
+	.gettstamp     = sock_gettstamp,
 	.listen        = sock_no_listen,
 	.shutdown      = sock_no_shutdown,
 	.setsockopt    = raw_setsockopt,
diff --git a/net/compat.c b/net/compat.c
index 47a614b370cd..e5456dd4c7a5 100644
--- a/net/compat.c
+++ b/net/compat.c
@@ -455,60 +455,6 @@ static int compat_sock_getsockopt(struct socket *sock, int level, int optname,
 	return sock_getsockopt(sock, level, optname, optval, optlen);
 }
 
-int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
-{
-	struct compat_timeval __user *ctv;
-	int err;
-	struct timeval tv;
-
-	if (COMPAT_USE_64BIT_TIME)
-		return sock_get_timestamp(sk, userstamp);
-
-	ctv = (struct compat_timeval __user *) userstamp;
-	err = -ENOENT;
-	sock_enable_timestamp(sk, SOCK_TIMESTAMP);
-	tv = ktime_to_timeval(sk->sk_stamp);
-	if (tv.tv_sec == -1)
-		return err;
-	if (tv.tv_sec == 0) {
-		sk->sk_stamp = ktime_get_real();
-		tv = ktime_to_timeval(sk->sk_stamp);
-	}
-	err = 0;
-	if (put_user(tv.tv_sec, &ctv->tv_sec) ||
-			put_user(tv.tv_usec, &ctv->tv_usec))
-		err = -EFAULT;
-	return err;
-}
-EXPORT_SYMBOL(compat_sock_get_timestamp);
-
-int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
-{
-	struct compat_timespec __user *ctv;
-	int err;
-	struct timespec ts;
-
-	if (COMPAT_USE_64BIT_TIME)
-		return sock_get_timestampns (sk, userstamp);
-
-	ctv = (struct compat_timespec __user *) userstamp;
-	err = -ENOENT;
-	sock_enable_timestamp(sk, SOCK_TIMESTAMP);
-	ts = ktime_to_timespec(sk->sk_stamp);
-	if (ts.tv_sec == -1)
-		return err;
-	if (ts.tv_sec == 0) {
-		sk->sk_stamp = ktime_get_real();
-		ts = ktime_to_timespec(sk->sk_stamp);
-	}
-	err = 0;
-	if (put_user(ts.tv_sec, &ctv->tv_sec) ||
-			put_user(ts.tv_nsec, &ctv->tv_nsec))
-		err = -EFAULT;
-	return err;
-}
-EXPORT_SYMBOL(compat_sock_get_timestampns);
-
 static int __compat_sys_getsockopt(int fd, int level, int optname,
 				   char __user *optval,
 				   int __user *optlen)
diff --git a/net/core/sock.c b/net/core/sock.c
index 3730eb855095..df17bbfaca27 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -2897,37 +2897,31 @@ bool lock_sock_fast(struct sock *sk)
 }
 EXPORT_SYMBOL(lock_sock_fast);
 
-int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
+int sock_gettstamp(struct socket *sock, void __user *userstamp,
+		   bool timeval, bool time32)
 {
-	struct timeval tv;
-
-	sock_enable_timestamp(sk, SOCK_TIMESTAMP);
-	tv = ktime_to_timeval(sk->sk_stamp);
-	if (tv.tv_sec == -1)
-		return -ENOENT;
-	if (tv.tv_sec == 0) {
-		sk->sk_stamp = ktime_get_real();
-		tv = ktime_to_timeval(sk->sk_stamp);
-	}
-	return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
-}
-EXPORT_SYMBOL(sock_get_timestamp);
-
-int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
-{
-	struct timespec ts;
+	struct sock *sk = sock->sk;
+	struct timespec64 ts;
 
 	sock_enable_timestamp(sk, SOCK_TIMESTAMP);
-	ts = ktime_to_timespec(sk->sk_stamp);
+	ts = ktime_to_timespec64(sk->sk_stamp);
 	if (ts.tv_sec == -1)
 		return -ENOENT;
 	if (ts.tv_sec == 0) {
 		sk->sk_stamp = ktime_get_real();
-		ts = ktime_to_timespec(sk->sk_stamp);
+		ts = ktime_to_timespec64(sk->sk_stamp);
 	}
-	return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
+
+	if (timeval)
+		ts.tv_nsec /= 1000;
+#ifdef CONFIG_COMPAT_32BIT_TIME
+	if (time32)
+		return put_old_timespec32(&ts, userstamp);
+#endif
+
+	return put_timespec64(&ts, userstamp);
 }
-EXPORT_SYMBOL(sock_get_timestampns);
+EXPORT_SYMBOL(sock_gettstamp);
 
 void sock_enable_timestamp(struct sock *sk, int flag)
 {
diff --git a/net/dccp/ipv4.c b/net/dccp/ipv4.c
index b08feb219b44..8103f3525773 100644
--- a/net/dccp/ipv4.c
+++ b/net/dccp/ipv4.c
@@ -986,6 +986,7 @@ static const struct proto_ops inet_dccp_ops = {
 	/* FIXME: work on tcp_poll to rename it to inet_csk_poll */
 	.poll		   = dccp_poll,
 	.ioctl		   = inet_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	/* FIXME: work on inet_listen to rename it to sock_common_listen */
 	.listen		   = inet_dccp_listen,
 	.shutdown	   = inet_shutdown,
diff --git a/net/dccp/ipv6.c b/net/dccp/ipv6.c
index 6344f1b18a6a..dacdb5b2638d 100644
--- a/net/dccp/ipv6.c
+++ b/net/dccp/ipv6.c
@@ -1072,6 +1072,7 @@ static const struct proto_ops inet6_dccp_ops = {
 	.getname	   = inet6_getname,
 	.poll		   = dccp_poll,
 	.ioctl		   = inet6_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = inet_dccp_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
diff --git a/net/ieee802154/socket.c b/net/ieee802154/socket.c
index bc6b912603f1..ce2dfb997537 100644
--- a/net/ieee802154/socket.c
+++ b/net/ieee802154/socket.c
@@ -164,10 +164,6 @@ static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
 	struct sock *sk = sock->sk;
 
 	switch (cmd) {
-	case SIOCGSTAMP:
-		return sock_get_timestamp(sk, (struct timeval __user *)arg);
-	case SIOCGSTAMPNS:
-		return sock_get_timestampns(sk, (struct timespec __user *)arg);
 	case SIOCGIFADDR:
 	case SIOCSIFADDR:
 		return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
@@ -426,6 +422,7 @@ static const struct proto_ops ieee802154_raw_ops = {
 	.getname	   = sock_no_getname,
 	.poll		   = datagram_poll,
 	.ioctl		   = ieee802154_sock_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,
 	.shutdown	   = sock_no_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
@@ -988,6 +985,7 @@ static const struct proto_ops ieee802154_dgram_ops = {
 	.getname	   = sock_no_getname,
 	.poll		   = datagram_poll,
 	.ioctl		   = ieee802154_sock_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,
 	.shutdown	   = sock_no_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index 20fda8fb8ffd..3490275bab50 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -911,12 +911,6 @@ int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 	struct rtentry rt;
 
 	switch (cmd) {
-	case SIOCGSTAMP:
-		err = sock_get_timestamp(sk, (struct timeval __user *)arg);
-		break;
-	case SIOCGSTAMPNS:
-		err = sock_get_timestampns(sk, (struct timespec __user *)arg);
-		break;
 	case SIOCADDRT:
 	case SIOCDELRT:
 		if (copy_from_user(&rt, p, sizeof(struct rtentry)))
@@ -988,6 +982,7 @@ const struct proto_ops inet_stream_ops = {
 	.getname	   = inet_getname,
 	.poll		   = tcp_poll,
 	.ioctl		   = inet_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = inet_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
@@ -1023,6 +1018,7 @@ const struct proto_ops inet_dgram_ops = {
 	.getname	   = inet_getname,
 	.poll		   = udp_poll,
 	.ioctl		   = inet_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
@@ -1055,6 +1051,7 @@ static const struct proto_ops inet_sockraw_ops = {
 	.getname	   = inet_getname,
 	.poll		   = datagram_poll,
 	.ioctl		   = inet_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
index 673bba31eb18..77f9716958a7 100644
--- a/net/ipv6/af_inet6.c
+++ b/net/ipv6/af_inet6.c
@@ -532,12 +532,6 @@ int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 	struct net *net = sock_net(sk);
 
 	switch (cmd) {
-	case SIOCGSTAMP:
-		return sock_get_timestamp(sk, (struct timeval __user *)arg);
-
-	case SIOCGSTAMPNS:
-		return sock_get_timestampns(sk, (struct timespec __user *)arg);
-
 	case SIOCADDRT:
 	case SIOCDELRT:
 
@@ -570,6 +564,7 @@ const struct proto_ops inet6_stream_ops = {
 	.getname	   = inet6_getname,
 	.poll		   = tcp_poll,			/* ok		*/
 	.ioctl		   = inet6_ioctl,		/* must change  */
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = inet_listen,		/* ok		*/
 	.shutdown	   = inet_shutdown,		/* ok		*/
 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
@@ -603,6 +598,7 @@ const struct proto_ops inet6_dgram_ops = {
 	.getname	   = inet6_getname,
 	.poll		   = udp_poll,			/* ok		*/
 	.ioctl		   = inet6_ioctl,		/* must change  */
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,		/* ok		*/
 	.shutdown	   = inet_shutdown,		/* ok		*/
 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c
index 413d98bf24f4..a913ccfff021 100644
--- a/net/ipv6/raw.c
+++ b/net/ipv6/raw.c
@@ -1344,6 +1344,7 @@ const struct proto_ops inet6_sockraw_ops = {
 	.getname	   = inet6_getname,
 	.poll		   = datagram_poll,		/* ok		*/
 	.ioctl		   = inet6_ioctl,		/* must change  */
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,		/* ok		*/
 	.shutdown	   = inet_shutdown,		/* ok		*/
 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
diff --git a/net/l2tp/l2tp_ip.c b/net/l2tp/l2tp_ip.c
index 35f6f86d4dcc..b7b844d9edec 100644
--- a/net/l2tp/l2tp_ip.c
+++ b/net/l2tp/l2tp_ip.c
@@ -615,6 +615,7 @@ static const struct proto_ops l2tp_ip_ops = {
 	.getname	   = l2tp_ip_getname,
 	.poll		   = datagram_poll,
 	.ioctl		   = inet_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c
index 237f1a4a0b0c..c379ebfa4cb7 100644
--- a/net/l2tp/l2tp_ip6.c
+++ b/net/l2tp/l2tp_ip6.c
@@ -751,6 +751,7 @@ static const struct proto_ops l2tp_ip6_ops = {
 	.getname	   = l2tp_ip6_getname,
 	.poll		   = datagram_poll,
 	.ioctl		   = inet6_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sock_no_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
diff --git a/net/netrom/af_netrom.c b/net/netrom/af_netrom.c
index 03f37c4e64fe..687103e0a3c8 100644
--- a/net/netrom/af_netrom.c
+++ b/net/netrom/af_netrom.c
@@ -1194,7 +1194,6 @@ static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 {
 	struct sock *sk = sock->sk;
 	void __user *argp = (void __user *)arg;
-	int ret;
 
 	switch (cmd) {
 	case TIOCOUTQ: {
@@ -1220,18 +1219,6 @@ static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		return put_user(amount, (int __user *)argp);
 	}
 
-	case SIOCGSTAMP:
-		lock_sock(sk);
-		ret = sock_get_timestamp(sk, argp);
-		release_sock(sk);
-		return ret;
-
-	case SIOCGSTAMPNS:
-		lock_sock(sk);
-		ret = sock_get_timestampns(sk, argp);
-		release_sock(sk);
-		return ret;
-
 	case SIOCGIFADDR:
 	case SIOCSIFADDR:
 	case SIOCGIFDSTADDR:
@@ -1357,6 +1344,7 @@ static const struct proto_ops nr_proto_ops = {
 	.getname	=	nr_getname,
 	.poll		=	datagram_poll,
 	.ioctl		=	nr_ioctl,
+	.gettstamp	=	sock_gettstamp,
 	.listen		=	nr_listen,
 	.shutdown	=	sock_no_shutdown,
 	.setsockopt	=	nr_setsockopt,
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 5610061e7f2e..06097f1e060b 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -4053,11 +4053,6 @@ static int packet_ioctl(struct socket *sock, unsigned int cmd,
 		spin_unlock_bh(&sk->sk_receive_queue.lock);
 		return put_user(amount, (int __user *)arg);
 	}
-	case SIOCGSTAMP:
-		return sock_get_timestamp(sk, (struct timeval __user *)arg);
-	case SIOCGSTAMPNS:
-		return sock_get_timestampns(sk, (struct timespec __user *)arg);
-
 #ifdef CONFIG_INET
 	case SIOCADDRT:
 	case SIOCDELRT:
@@ -4415,6 +4410,7 @@ static const struct proto_ops packet_ops_spkt = {
 	.getname =	packet_getname_spkt,
 	.poll =		datagram_poll,
 	.ioctl =	packet_ioctl,
+	.gettstamp =	sock_gettstamp,
 	.listen =	sock_no_listen,
 	.shutdown =	sock_no_shutdown,
 	.setsockopt =	sock_no_setsockopt,
@@ -4436,6 +4432,7 @@ static const struct proto_ops packet_ops = {
 	.getname =	packet_getname,
 	.poll =		packet_poll,
 	.ioctl =	packet_ioctl,
+	.gettstamp =	sock_gettstamp,
 	.listen =	sock_no_listen,
 	.shutdown =	sock_no_shutdown,
 	.setsockopt =	packet_setsockopt,
diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
index 86e1e37eb4e8..9da159f3fc2a 100644
--- a/net/qrtr/qrtr.c
+++ b/net/qrtr/qrtr.c
@@ -967,9 +967,6 @@ static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 			break;
 		}
 		break;
-	case SIOCGSTAMP:
-		rc = sock_get_timestamp(sk, argp);
-		break;
 	case SIOCADDRT:
 	case SIOCDELRT:
 	case SIOCSIFADDR:
@@ -1032,6 +1029,7 @@ static const struct proto_ops qrtr_proto_ops = {
 	.recvmsg	= qrtr_recvmsg,
 	.getname	= qrtr_getname,
 	.ioctl		= qrtr_ioctl,
+	.gettstamp	= sock_gettstamp,
 	.poll		= datagram_poll,
 	.shutdown	= sock_no_shutdown,
 	.setsockopt	= sock_no_setsockopt,
diff --git a/net/rose/af_rose.c b/net/rose/af_rose.c
index d00a0ef39a56..64bcbb22c8f7 100644
--- a/net/rose/af_rose.c
+++ b/net/rose/af_rose.c
@@ -1299,12 +1299,6 @@ static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		return put_user(amount, (unsigned int __user *) argp);
 	}
 
-	case SIOCGSTAMP:
-		return sock_get_timestamp(sk, (struct timeval __user *) argp);
-
-	case SIOCGSTAMPNS:
-		return sock_get_timestampns(sk, (struct timespec __user *) argp);
-
 	case SIOCGIFADDR:
 	case SIOCSIFADDR:
 	case SIOCGIFDSTADDR:
@@ -1472,6 +1466,7 @@ static const struct proto_ops rose_proto_ops = {
 	.getname	=	rose_getname,
 	.poll		=	datagram_poll,
 	.ioctl		=	rose_ioctl,
+	.gettstamp	=	sock_gettstamp,
 	.listen		=	rose_listen,
 	.shutdown	=	sock_no_shutdown,
 	.setsockopt	=	rose_setsockopt,
diff --git a/net/sctp/ipv6.c b/net/sctp/ipv6.c
index fc6c5e4bffa5..62da13b888e0 100644
--- a/net/sctp/ipv6.c
+++ b/net/sctp/ipv6.c
@@ -1028,6 +1028,7 @@ static const struct proto_ops inet6_seqpacket_ops = {
 	.getname	   = sctp_getname,
 	.poll		   = sctp_poll,
 	.ioctl		   = inet6_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sctp_inet_listen,
 	.shutdown	   = inet_shutdown,
 	.setsockopt	   = sock_common_setsockopt,
diff --git a/net/sctp/protocol.c b/net/sctp/protocol.c
index e948db29ab53..b640eeedc8b4 100644
--- a/net/sctp/protocol.c
+++ b/net/sctp/protocol.c
@@ -1026,6 +1026,7 @@ static const struct proto_ops inet_seqpacket_ops = {
 	.getname	   = inet_getname,	/* Semantics are different.  */
 	.poll		   = sctp_poll,
 	.ioctl		   = inet_ioctl,
+	.gettstamp	   = sock_gettstamp,
 	.listen		   = sctp_inet_listen,
 	.shutdown	   = inet_shutdown,	/* Looks harmless.  */
 	.setsockopt	   = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
diff --git a/net/socket.c b/net/socket.c
index b9d71b503720..6814e8dc8af1 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -1069,6 +1069,15 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
 
 			err = open_related_ns(&net->ns, get_net_ns);
 			break;
+		case SIOCGSTAMP:
+		case SIOCGSTAMPNS:
+			if (!sock->ops->gettstamp) {
+				err = -ENOIOCTLCMD;
+				break;
+			}
+			err = sock->ops->gettstamp(sock, argp,
+						   cmd == SIOCGSTAMP, false);
+			break;
 		default:
 			err = sock_do_ioctl(net, sock, cmd, arg);
 			break;
@@ -2740,38 +2749,6 @@ void socket_seq_show(struct seq_file *seq)
 #endif				/* CONFIG_PROC_FS */
 
 #ifdef CONFIG_COMPAT
-static int do_siocgstamp(struct net *net, struct socket *sock,
-			 unsigned int cmd, void __user *up)
-{
-	mm_segment_t old_fs = get_fs();
-	struct timeval ktv;
-	int err;
-
-	set_fs(KERNEL_DS);
-	err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
-	set_fs(old_fs);
-	if (!err)
-		err = compat_put_timeval(&ktv, up);
-
-	return err;
-}
-
-static int do_siocgstampns(struct net *net, struct socket *sock,
-			   unsigned int cmd, void __user *up)
-{
-	mm_segment_t old_fs = get_fs();
-	struct timespec kts;
-	int err;
-
-	set_fs(KERNEL_DS);
-	err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
-	set_fs(old_fs);
-	if (!err)
-		err = compat_put_timespec(&kts, up);
-
-	return err;
-}
-
 static int compat_dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
 {
 	struct compat_ifconf ifc32;
@@ -3119,9 +3096,12 @@ static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
 	case SIOCDELRT:
 		return routing_ioctl(net, sock, cmd, argp);
 	case SIOCGSTAMP:
-		return do_siocgstamp(net, sock, cmd, argp);
 	case SIOCGSTAMPNS:
-		return do_siocgstampns(net, sock, cmd, argp);
+		if (!sock->ops->gettstamp)
+			return -ENOIOCTLCMD;
+		return sock->ops->gettstamp(sock, argp, cmd == SIOCGSTAMP,
+					    !COMPAT_USE_64BIT_TIME);
+
 	case SIOCBONDSLAVEINFOQUERY:
 	case SIOCBONDINFOQUERY:
 	case SIOCSHWTSTAMP:
diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
index d49aa79b7997..fe2673d17009 100644
--- a/net/x25/af_x25.c
+++ b/net/x25/af_x25.c
@@ -1388,18 +1388,6 @@ static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		break;
 	}
 
-	case SIOCGSTAMP:
-		rc = -EINVAL;
-		if (sk)
-			rc = sock_get_timestamp(sk,
-						(struct timeval __user *)argp);
-		break;
-	case SIOCGSTAMPNS:
-		rc = -EINVAL;
-		if (sk)
-			rc = sock_get_timestampns(sk,
-					(struct timespec __user *)argp);
-		break;
 	case SIOCGIFADDR:
 	case SIOCSIFADDR:
 	case SIOCGIFDSTADDR:
@@ -1671,8 +1659,6 @@ static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
 				unsigned long arg)
 {
 	void __user *argp = compat_ptr(arg);
-	struct sock *sk = sock->sk;
-
 	int rc = -ENOIOCTLCMD;
 
 	switch(cmd) {
@@ -1680,18 +1666,6 @@ static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
 	case TIOCINQ:
 		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
 		break;
-	case SIOCGSTAMP:
-		rc = -EINVAL;
-		if (sk)
-			rc = compat_sock_get_timestamp(sk,
-					(struct timeval __user*)argp);
-		break;
-	case SIOCGSTAMPNS:
-		rc = -EINVAL;
-		if (sk)
-			rc = compat_sock_get_timestampns(sk,
-					(struct timespec __user*)argp);
-		break;
 	case SIOCGIFADDR:
 	case SIOCSIFADDR:
 	case SIOCGIFDSTADDR:
@@ -1755,6 +1729,7 @@ static const struct proto_ops x25_proto_ops = {
 #ifdef CONFIG_COMPAT
 	.compat_ioctl = compat_x25_ioctl,
 #endif
+	.gettstamp =	sock_gettstamp,
 	.listen =	x25_listen,
 	.shutdown =	sock_no_shutdown,
 	.setsockopt =	x25_setsockopt,
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2019-04-17 20:16 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-16 20:32 [PATCH net-next 1/3] net: rework SIOCGSTAMP ioctl handling Arnd Bergmann
2019-04-17  9:35 ` Neil Horman
2019-04-17 17:21   ` David Miller
2019-04-17 20:15     ` Neil Horman
2019-04-17  9:59 ` Marc Kleine-Budde
2019-04-17 14:46 ` Willem de Bruijn
2019-04-17 16:19   ` Arnd Bergmann
2019-04-17 18:16     ` Willem de Bruijn
  -- strict thread matches above, loose matches on Subject: below --
2018-08-29 12:59 Arnd Bergmann
2018-08-30 20:09 ` Willem de Bruijn
2018-08-31 10:31   ` Arnd Bergmann
2018-08-31 13:37     ` Willem de Bruijn
2018-08-31 14:00       ` Arnd Bergmann
2018-08-31 15:08         ` Willem de Bruijn
2018-09-13 12:28 ` Arnd Bergmann
2018-09-21  9:14 ` Stefan Schmidt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).