Skip to contentSkip to conversation

net: convert remaining CAN protocols to getsockopt_iter

PATCH Superseded Breno Leitao 3 participants 8 messages May 7 – Aug 4

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover net: convert remaining CAN protocols to getsockopt_iter

Continue the conversion of socket protocols to the new getsockopt_iter API by covering the two remaining CAN implementations:

- isotp (CAN ISO-TP)
- j1939 (SAE J1939)

With these, all CAN protocols that expose a getsockopt callback (raw, isotp, j1939) now use .getsockopt_iter; raw was converted as part of an earlier series.

These are mechanical, ABI-preserving conversions following the same pattern as previously converted protocols (af_packet, can/raw, af_netlink, af_vsock):

- The (char __user *optval, int __user *optlen) pair is replaced with a single sockopt_t *opt that carries the buffer length on input and the returned size on output, and exposes an iov_iter for the copy-out path.
- put_user()/copy_to_user() pairs are replaced with a single copy_to_iter() per option.
- The wrapper in do_sock_getsockopt() handles writing optlen back to userspace.

No functional or ABI change is intended.

For more context about the motivation for this change, please check commit 67fab22a7ad ("net: add getsockopt_iter callback to proto_ops")

---
Breno Leitao (2):
can: j1939: convert to getsockopt_iter
can: isotp: convert to getsockopt_iter

 net/can/isotp.c        | 12 +++++-------
 net/can/j1939/socket.c | 21 +++++++++++++--------
 2 files changed, 18 insertions(+), 15 deletions(-)

---
base-commit: dacf281771a9aed1a723b196120a0de8637910b9
change-id: 20260507-getsock_two_can-5d3604b1982f

Best regards,

Signature · 1 line
Breno Leitao <[email protected]>
#1 lore raw

PATCH 1/2 can: j1939: convert to getsockopt_iter

Convert CAN J1939 socket's getsockopt implementation to use the new getsockopt_iter callback with sockopt_t.

Key changes:
- Replace (char __user *optval, int __user *optlen) with sockopt_t *opt
- Use opt->optlen for buffer length (input) and returned size (output)
- Use copy_to_iter() instead of copy_to_user()
- Restructure the chained if/else if (which depended on put_user() being an expression) into a nested if/else block now that opt->optlen = len is a statement
- Add linux/uio.h for copy_to_iter()

---
 net/can/j1939/socket.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
net/can/j1939/socket.c +13 -8
index 50a598ef5fd4a..d0c6ce607b0dc 100644
@@ -17,6 +17,7 @@
 #include <linux/can/skb.h>
 #include <linux/errqueue.h>
 #include <linux/if_arp.h>
+#include <linux/uio.h>
 #include <net/can.h>
 
 #include "j1939-priv.h"
@@ -767,7 +768,7 @@ static int j1939_sk_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int j1939_sk_getsockopt(struct socket *sock, int level, int optname,
-			       char __user *optval, int __user *optlen)
+			       sockopt_t *opt)
 {
 	struct sock *sk = sock->sk;
 	struct j1939_sock *jsk = j1939_sk(sk);
@@ -779,8 +780,7 @@ static int j1939_sk_getsockopt(struct socket *sock, int level, int optname,
 
 	if (level != SOL_CAN_J1939)
 		return -EINVAL;
-	if (get_user(ulen, optlen))
-		return -EFAULT;
+	ulen = opt->optlen;
 	if (ulen < 0)
 		return -EINVAL;
 
@@ -804,11 +804,16 @@ static int j1939_sk_getsockopt(struct socket *sock, int level, int optname,
 	 * but most sockopt's are 'int' properties, and have 'len' & 'val'
 	 * left unchanged, but instead modified 'tmp'
 	 */
-	if (len > ulen)
-		ret = -EFAULT;
-	else if (put_user(len, optlen))
+	if (len > ulen) {
 		ret = -EFAULT;
-	else if (copy_to_user(optval, val, len))
+		goto no_copy;
+	}
+
+	opt->optlen = len;
+	/* Even if the copy below fails, we want to update optlen. This is
+	 * a bit confusing, but, it preserves the original behaviour
+	 */
+	if (copy_to_iter(val, len, &opt->iter_out) != len)
 		ret = -EFAULT;
 	else
 		ret = 0;
@@ -1385,7 +1390,7 @@ static const struct proto_ops j1939_ops = {
 	.listen = sock_no_listen,
 	.shutdown = sock_no_shutdown,
 	.setsockopt = j1939_sk_setsockopt,
-	.getsockopt = j1939_sk_getsockopt,
+	.getsockopt_iter = j1939_sk_getsockopt,
 	.sendmsg = j1939_sk_sendmsg,
 	.recvmsg = j1939_sk_recvmsg,
 	.mmap = sock_no_mmap,
Signature · 1 line
2.52.0
maintainer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH net-next 1/2] can: j1939: convert to getsockopt_iter

Quoted 12 lines · Breno Leitao

Convert CAN J1939 socket's getsockopt implementation to use the new getsockopt_iter callback with sockopt_t.

Key changes:
- Replace (char __user *optval, int __user *optlen) with sockopt_t *opt
- Use opt->optlen for buffer length (input) and returned size (output)
- Use copy_to_iter() instead of copy_to_user()
- Restructure the chained if/else if (which depended on put_user() being an expression) into a nested if/else block now that opt->optlen = len is a statement
- Add linux/uio.h for copy_to_iter()

Quoted 1 line

LGTM, Thank you!

Best Regards,
Oleksij

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#3 lore raw

PATCH 2/2 can: isotp: convert to getsockopt_iter

In reply to Breno Leitao

Convert CAN ISO-TP socket's getsockopt implementation to use the new getsockopt_iter callback with sockopt_t.

Key changes:
- Replace (char __user *optval, int __user *optlen) with sockopt_t *opt
- Use opt->optlen for buffer length (input) and returned size (output)
- Use copy_to_iter() instead of put_user()/copy_to_user()

---
 net/can/isotp.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
net/can/isotp.c +5 -7
index c48b4a818297e..1c33f09fbd338 100644
@@ -1500,7 +1500,7 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int isotp_getsockopt(struct socket *sock, int level, int optname,
-			    char __user *optval, int __user *optlen)
+			    sockopt_t *opt)
 {
 	struct sock *sk = sock->sk;
 	struct isotp_sock *so = isotp_sk(sk);
@@ -1509,8 +1509,7 @@ static int isotp_getsockopt(struct socket *sock, int level, int optname,
 
 	if (level != SOL_CAN_ISOTP)
 		return -EINVAL;
-	if (get_user(len, optlen))
-		return -EFAULT;
+	len = opt->optlen;
 	if (len < 0)
 		return -EINVAL;
 
@@ -1544,9 +1543,8 @@ static int isotp_getsockopt(struct socket *sock, int level, int optname,
 		return -ENOPROTOOPT;
 	}
 
-	if (put_user(len, optlen))
-		return -EFAULT;
-	if (copy_to_user(optval, val, len))
+	opt->optlen = len;
+	if (copy_to_iter(val, len, &opt->iter_out) != len)
 		return -EFAULT;
 	return 0;
 }
@@ -1718,7 +1716,7 @@ static const struct proto_ops isotp_ops = {
 	.listen = sock_no_listen,
 	.shutdown = sock_no_shutdown,
 	.setsockopt = isotp_setsockopt,
-	.getsockopt = isotp_getsockopt,
+	.getsockopt_iter = isotp_getsockopt,
 	.sendmsg = isotp_sendmsg,
 	.recvmsg = isotp_recvmsg,
 	.mmap = sock_no_mmap,
Signature · 1 line
2.52.0
maintainer Acked-by on v1 2/2
#4 lore raw

Re: [PATCH net-next 2/2] can: isotp: convert to getsockopt_iter

Quoted 50 lines · Breno Leitao

Convert CAN ISO-TP socket's getsockopt implementation to use the new getsockopt_iter callback with sockopt_t.

Key changes:
- Replace (char __user *optval, int __user *optlen) with sockopt_t *opt
- Use opt->optlen for buffer length (input) and returned size (output)
- Use copy_to_iter() instead of put_user()/copy_to_user()

---
  net/can/isotp.c | 12 +++++-------
  1 file changed, 5 insertions(+), 7 deletions(-)
net/can/isotp.c +4 -6
index c48b4a818297e..1c33f09fbd338 100644
@@ -1500,7 +1500,7 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname,
  }
 
  static int isotp_getsockopt(struct socket *sock, int level, int optname,
-			    char __user *optval, int __user *optlen)
+			    sockopt_t *opt)
  {
  	struct sock *sk = sock->sk;
  	struct isotp_sock *so = isotp_sk(sk);
@@ -1509,8 +1509,7 @@ static int isotp_getsockopt(struct socket *sock, int level, int optname,
 
  	if (level != SOL_CAN_ISOTP)
  		return -EINVAL;
-	if (get_user(len, optlen))
-		return -EFAULT;
+	len = opt->optlen;
  	if (len < 0)
  		return -EINVAL;
 
@@ -1544,9 +1543,8 @@ static int isotp_getsockopt(struct socket *sock, int level, int optname,
  		return -ENOPROTOOPT;
  	}
 
-	if (put_user(len, optlen))
-		return -EFAULT;
-	if (copy_to_user(optval, val, len))
+	opt->optlen = len;
+	if (copy_to_iter(val, len, &opt->iter_out) != len)
  		return -EFAULT;
  	return 0;
  }

net/can/isotp.c static const struct proto_ops isotp_ops = { · line 1722

Quoted 8 lines
net/can/isotp.c +1 -1
@@ -1718,7 +1716,7 @@ static const struct proto_ops isotp_ops = {
  	.listen = sock_no_listen,
  	.shutdown = sock_no_shutdown,
  	.setsockopt = isotp_setsockopt,
-	.getsockopt = isotp_getsockopt,
+	.getsockopt_iter = isotp_getsockopt,
  	.sendmsg = isotp_sendmsg,
  	.recvmsg = isotp_recvmsg,
  	.mmap = sock_no_mmap,

Same pattern as in net/can/raw.c that had slipped in via net-next last time ;-)

Thanks Breno!

Best regards,
Oliver

#5 lore raw

Re: [PATCH net-next 2/2] can: isotp: convert to getsockopt_iter

Quoted 61 lines · Oliver Hartkopp
Quoted 46 lines · Breno Leitao

Convert CAN ISO-TP socket's getsockopt implementation to use the new getsockopt_iter callback with sockopt_t.

Key changes:
- Replace (char __user *optval, int __user *optlen) with sockopt_t *opt
- Use opt->optlen for buffer length (input) and returned size (output)
- Use copy_to_iter() instead of put_user()/copy_to_user()

---
  net/can/isotp.c | 12 +++++-------
  1 file changed, 5 insertions(+), 7 deletions(-)
net/can/isotp.c +4 -6
index c48b4a818297e..1c33f09fbd338 100644
@@ -1500,7 +1500,7 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname,
  }
  static int isotp_getsockopt(struct socket *sock, int level, int optname,
-			    char __user *optval, int __user *optlen)
+			    sockopt_t *opt)
  {
  	struct sock *sk = sock->sk;
  	struct isotp_sock *so = isotp_sk(sk);
@@ -1509,8 +1509,7 @@ static int isotp_getsockopt(struct socket *sock, int level, int optname,
  	if (level != SOL_CAN_ISOTP)
  		return -EINVAL;
-	if (get_user(len, optlen))
-		return -EFAULT;
+	len = opt->optlen;
  	if (len < 0)
  		return -EINVAL;
@@ -1544,9 +1543,8 @@ static int isotp_getsockopt(struct socket *sock, int level, int optname,
  		return -ENOPROTOOPT;
  	}
-	if (put_user(len, optlen))
-		return -EFAULT;
-	if (copy_to_user(optval, val, len))
+	opt->optlen = len;
+	if (copy_to_iter(val, len, &opt->iter_out) != len)
  		return -EFAULT;
  	return 0;
  }

net/can/isotp.c static const struct proto_ops isotp_ops = { · line 1722

Quoted 8 lines
net/can/isotp.c +1 -1
@@ -1718,7 +1716,7 @@ static const struct proto_ops isotp_ops = {
  	.listen = sock_no_listen,
  	.shutdown = sock_no_shutdown,
  	.setsockopt = isotp_setsockopt,
-	.getsockopt = isotp_getsockopt,
+	.getsockopt_iter = isotp_getsockopt,
  	.sendmsg = isotp_sendmsg,
  	.recvmsg = isotp_recvmsg,
  	.mmap = sock_no_mmap,

Same pattern as in net/can/raw.c that had slipped in via net-next last time ;-)

Quoted 3 lines

Thanks Breno!

Thanks for the review!

I haven't seen this on linux-next yet. Please let me know if there is any update that is expected from my side.

--breno

maintainer
#6 lore raw

Re: [PATCH net-next 2/2] can: isotp: convert to getsockopt_iter

Quoted 62 lines · Breno Leitao
Quoted 61 lines · Oliver Hartkopp
Quoted 46 lines · Breno Leitao

Convert CAN ISO-TP socket's getsockopt implementation to use the new getsockopt_iter callback with sockopt_t.

Key changes:
- Replace (char __user *optval, int __user *optlen) with sockopt_t *opt
- Use opt->optlen for buffer length (input) and returned size (output)
- Use copy_to_iter() instead of put_user()/copy_to_user()

---
   net/can/isotp.c | 12 +++++-------
   1 file changed, 5 insertions(+), 7 deletions(-)
net/can/isotp.c +4 -6
index c48b4a818297e..1c33f09fbd338 100644
@@ -1500,7 +1500,7 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname,
   }
   static int isotp_getsockopt(struct socket *sock, int level, int optname,
-			    char __user *optval, int __user *optlen)
+			    sockopt_t *opt)
   {
   	struct sock *sk = sock->sk;
   	struct isotp_sock *so = isotp_sk(sk);
@@ -1509,8 +1509,7 @@ static int isotp_getsockopt(struct socket *sock, int level, int optname,
   	if (level != SOL_CAN_ISOTP)
   		return -EINVAL;
-	if (get_user(len, optlen))
-		return -EFAULT;
+	len = opt->optlen;
   	if (len < 0)
   		return -EINVAL;
@@ -1544,9 +1543,8 @@ static int isotp_getsockopt(struct socket *sock, int level, int optname,
   		return -ENOPROTOOPT;
   	}
-	if (put_user(len, optlen))
-		return -EFAULT;
-	if (copy_to_user(optval, val, len))
+	opt->optlen = len;
+	if (copy_to_iter(val, len, &opt->iter_out) != len)
   		return -EFAULT;
   	return 0;
   }

net/can/isotp.c static const struct proto_ops isotp_ops = { · line 1722

Quoted 8 lines
net/can/isotp.c +1 -1
@@ -1718,7 +1716,7 @@ static const struct proto_ops isotp_ops = {
   	.listen = sock_no_listen,
   	.shutdown = sock_no_shutdown,
   	.setsockopt = isotp_setsockopt,
-	.getsockopt = isotp_getsockopt,
+	.getsockopt_iter = isotp_getsockopt,
   	.sendmsg = isotp_sendmsg,
   	.recvmsg = isotp_recvmsg,
   	.mmap = sock_no_mmap,

Same pattern as in net/can/raw.c that had slipped in via net-next last time ;-)

Quoted 8 lines
Quoted 3 lines

Thanks Breno!

Thanks for the review!

I haven't seen this on linux-next yet. Please let me know if there is any update that is expected from my side.

This patch will likely go upstream via can-next.

Marc currently seems to be busy.

Best regards,
Oliver

#7 lore raw

Re: [PATCH net-next 2/2] can: isotp: convert to getsockopt_iter

Quoted 8 lines · Oliver Hartkopp
Quoted 8 lines
Quoted 7 lines

Same pattern as in net/can/raw.c that had slipped in via net-next last time ;-)

Thanks Breno!

Quoted 8 lines
Quoted 4 lines

Thanks for the review!

I haven't seen this on linux-next yet. Please let me know if there is any update that is expected from my side.

This patch will likely go upstream via can-next.

Marc currently seems to be busy.

Hello, any chance that Marc is not busy anymore?

This is the last leaf nodes that is missing in this conversion now.

https://lore.kernel.org/all/[email protected]/

Thanks,
--breno