net/smc: convert getsockopt to getsockopt_iter
Subsystems (6)
cover net/smc: convert getsockopt to getsockopt_iter
SMC is the last protocol to be converted to getsockopt_iter(). As soon as this series lands, and all the other outstanding conversion patches land, getsockopt_iter() will be renamed back to getsockopt() and the first part of the conversion will be complete.
This series converts SMC's getsockopt() to the new getsockopt_iter() callback, which hands protocols a sockopt_t that abstracts the optval buffer as an iov_iter and the option length as a plain int instead of raw (char __user *optval, int __user *optlen) pointers. This is part of the broader effort to move getsockopt() off __user pointers so that kernel-backed callers (e.g. io_uring) can issue socket options.
SMC is a proxy socket: only the SOL_SMC level is handled locally, while every other level is forwarded to the underlying CLC (TCP) socket. That sub-socket's getsockopt() still operates on __user buffers, so the pass-through cannot simply forward the sockopt_t. Instead it reconstructs optval from a user-backed iter_out, forwards the preserved user optlen pointer (kept in sockopt_t by the base patch), and mirrors the length reported by the clcsock back into opt->optlen.
Because of that, the SOL_SMC level is fully converted and works with any iov_iter type, but the CLC pass-through is intentionally limited to user-backed iters (ubuf) and returns -EOPNOTSUPP otherwise. This is a deliberate, temporary restriction: SMC will continue to operate on ubuf only until the underlying protocols (TCP/IP) grow their own getsockopt_iter() callbacks. Once that generic callback path is enabled, the pass-through can forward the sockopt_t directly and drop the ubuf restriction, supporting all iov_iter types.
The series contains:
1) smc: convert getsockopt to getsockopt_iter / sockopt_t.
2) selftests: net: I've vibe coded a kselftest exercising both the SOL_SMC path and the CLC pass-through, including the oversized-buffer writeback.
---
Breno Leitao (2):
smc: convert to getsockopt_iter
selftests: net: add SMC getsockopt_iter conversion test
net/smc/af_smc.c | 41 +++++-- net/smc/smc.h | 2 +- net/smc/smc_inet.c | 4 +- tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/getsockopt_smc.c | 175 +++++++++++++++++++++++++++ 5 files changed, 208 insertions(+), 15 deletions(-)
---
base-commit: 0a8b288e2248cb62a62f748bc095c2136acf22b2
change-id: 20260604-getsockopt_smc-e2ad719486eb
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
PATCH 1/2 smc: convert to getsockopt_iter
Convert SMC 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()
- Add linux/uio.h for copy_to_iter()
SMC is a proxy socket: only the SOL_SMC level is handled locally, while all other levels are forwarded to the underlying CLC (TCP) socket. That socket's getsockopt() still operates on __user buffers, so the pass-through is limited to user-backed iters: optval is reconstructed from iter_out, the original optlen pointer (preserved in sockopt_t) is forwarded, and the length reported by the clcsock is mirrored back into opt->optlen so the core writes the correct value to userspace.
--- net/smc/af_smc.c | 41 +++++++++++++++++++++++++++++------------ net/smc/smc.h | 2 +- net/smc/smc_inet.c | 4 ++-- 3 files changed, 32 insertions(+), 15 deletions(-)
net/smc/af_smc.c +29 -12
@@ -27,6 +27,7 @@#include <linux/rcupdate_wait.h> #include <linux/ctype.h> #include <linux/splice.h> +#include <linux/uio.h> #include <net/sock.h> #include <net/inet_common.h>
@@ -3017,17 +3018,14 @@ int smc_shutdown(struct socket *sock, int how) }static int __smc_getsockopt (struct socket *sock,int level,int optname, -char __user *optval,int __user *optlen) +sockopt_t *opt) {struct smc_sock *smc;int val, len; smc =smc_sk (sock->sk); -if (get_user (len, optlen)) -return -EFAULT ; - - len =min_t (int, len,sizeof (int )); + len =min_t (int, opt->optlen,sizeof (int ));if (len <0 )return -EINVAL ;
@@ -3040,9 +3038,8 @@ static int __smc_getsockopt(struct socket *sock, int level, int optname,return -EOPNOTSUPP ; } -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 ;
@@ -3168,13 +3165,26 @@ int smc_setsockopt(struct socket *sock, int level, int optname, }int smc_getsockopt (struct socket *sock,int level,int optname, -char __user *optval,int __user *optlen) +sockopt_t *opt) {struct smc_sock *smc;int rc;if (level ==SOL_SMC ) -return __smc_getsockopt (sock, level, optname, optval, optlen); +return __smc_getsockopt (sock, level, optname, opt); + +/* Other levels apply to the CLC socket, whose getsockopt() still +* operates on __user buffers. Reconstruct the userspace pointers and +* forward the call; kernel-backed callers (e.g. io_uring) are not +* supported for this pass-through. +* +* TODO: this pass-through is limited to user-backed iters because the +* underlying protocols (TCP/IP) have not been converted to +* getsockopt_iter() yet. Once they are, forward the sockopt_t directly +* and drop this restriction so all iov_iter types are supported. +*/ +if (!iter_is_ubuf (&opt->iter_out) || !opt->optlen_user) +return -EOPNOTSUPP ; smc =smc_sk (sock->sk);mutex_lock (&smc->clcsock_release_lock);
@@ -3188,8 +3198,15 @@ int smc_getsockopt(struct socket *sock, int level, int optname,return -EOPNOTSUPP ; } rc = smc->clcsock->ops->getsockopt (smc->clcsock, level, optname, - optval, optlen); + opt->iter_out.ubuf, opt->optlen_user);mutex_unlock (&smc->clcsock_release_lock); + +/* The clcsock wrote the resulting length to the user optlen pointer; +* mirror it into opt->optlen so the core writes the same value back. +*/ +if (get_user (opt->optlen, opt->optlen_user)) +return -EFAULT ; +return rc; }
@@ -3341,7 +3358,7 @@ static const struct proto_ops smc_sock_ops = { .listen = smc_listen, .shutdown = smc_shutdown, .setsockopt = smc_setsockopt, - .getsockopt = smc_getsockopt, + .getsockopt_iter = smc_getsockopt, .sendmsg = smc_sendmsg, .recvmsg = smc_recvmsg, .mmap = sock_no_mmap,
net/smc/smc.h +1 -1
@@ -59,7 +59,7 @@ int smc_shutdown(struct socket *sock, int how);int smc_setsockopt (struct socket *sock,int level,int optname,sockptr_t optval,unsigned int optlen);int smc_getsockopt (struct socket *sock,int level,int optname, -char __user *optval,int __user *optlen); +sockopt_t *opt);int smc_sendmsg (struct socket *sock,struct msghdr *msg,size_t len);int smc_recvmsg (struct socket *sock,struct msghdr *msg,size_t len,int flags);
net/smc/smc_inet.c +2 -2
@@ -44,7 +44,7 @@ static const struct proto_ops smc_inet_stream_ops = { .listen = smc_listen, .shutdown = smc_shutdown, .setsockopt = smc_setsockopt, - .getsockopt = smc_getsockopt, + .getsockopt_iter = smc_getsockopt, .sendmsg = smc_sendmsg, .recvmsg = smc_recvmsg, .mmap = sock_no_mmap,
@@ -91,7 +91,7 @@ static const struct proto_ops smc_inet6_stream_ops = { .listen = smc_listen, .shutdown = smc_shutdown, .setsockopt = smc_setsockopt, - .getsockopt = smc_getsockopt, + .getsockopt_iter = smc_getsockopt, .sendmsg = smc_sendmsg, .recvmsg = smc_recvmsg, .mmap = sock_no_mmap,
Signature · 1 line
2.53.0-Meta
Re: [PATCH net-next 1/2] smc: convert to getsockopt_iter
Quoted 17 lines · Breno Leitao
Convert SMC socket's getsockopt implementation to use the new
getsockopt_itercallback withsockopt_t.Key changes:
- Replace (char__user*optval, int__user*optlen) withsockopt_t*opt
- Useopt->optlenfor buffer length (input) and returned size (output)
- Usecopy_to_iter()instead of put_user()/copy_to_user()
- Addlinux/uio.hforcopy_to_iter()SMC is a proxy socket: only the
SOL_SMClevel is handled locally, while all other levels are forwarded to the underlying CLC (TCP) socket. That socket'sgetsockopt()still operates on__userbuffers, so the pass-through is limited to user-backed iters: optval is reconstructed fromiter_out, the original optlen pointer (preserved insockopt_t) is forwarded, and the length reported by the clcsock is mirrored back intoopt->optlenso the core writes the correct value to userspace.
Quoted 1 line
Signed-off-by Breno Leitao <[email protected]>
Damn, this is not building. let me send a fixed version (v2).
--
pw-bot: cr
PATCH 2/2 selftests: net: add SMC getsockopt_iter conversion test
In reply to Breno LeitaoAdd a kselftest that exercises the SMC getsockopt() paths converted to the getsockopt_iter() / sockopt_t callback:
- SOL_SMC options (SMC_LIMIT_HS), handled directly by smc_getsockopt(), which returns the int value through copy_to_iter() and reports the written length in opt->optlen.
- The CLC pass-through (e.g. SOL_TCP), where smc_getsockopt() forwards to the underlying TCP socket: optval is reconstructed from iter_out, the optlen pointer is forwarded, and the clamped length is mirrored back through opt->optlen. The oversized-buffer case (input optlen differs from output) specifically guards against a missing writeback sync.
--- tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/getsockopt_smc.c | 175 +++++++++++++++++++++++++++ 2 files changed, 176 insertions(+)
tools/testing/selftests/net/Makefile +1 -0
@@ -177,6 +177,7 @@ TEST_GEN_PROGS := \ bind_wildcard \ epoll_busy_poll \ getsockopt_iter \ + getsockopt_smc \ icmp_rfc4884 \ ipv6_fragmentation \ proc_net_pktgen \
tools/testing/selftests/net/getsockopt_smc.c new file +175 -0
175 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta