sock: allow reading and changing sk_userlocks with setsockopt
authorPavel Tikhomirov <ptikhomirov@virtuozzo.com>
Wed, 4 Aug 2021 07:55:56 +0000 (10:55 +0300)
committerDavid S. Miller <davem@davemloft.net>
Wed, 4 Aug 2021 11:52:03 +0000 (12:52 +0100)
SOCK_SNDBUF_LOCK and SOCK_RCVBUF_LOCK flags disable automatic socket
buffers adjustment done by kernel (see tcp_fixup_rcvbuf() and
tcp_sndbuf_expand()). If we've just created a new socket this adjustment
is enabled on it, but if one changes the socket buffer size by
setsockopt(SO_{SND,RCV}BUF*) it becomes disabled.

CRIU needs to call setsockopt(SO_{SND,RCV}BUF*) on each socket on
restore as it first needs to increase buffer sizes for packet queues
restore and second it needs to restore back original buffer sizes. So
after CRIU restore all sockets become non-auto-adjustable, which can
decrease network performance of restored applications significantly.

CRIU need to be able to restore sockets with enabled/disabled adjustment
to the same state it was before dump, so let's add special setsockopt
for it.

Let's also export SOCK_SNDBUF_LOCK and SOCK_RCVBUF_LOCK flags to uAPI so
that using these interface one can reenable automatic socket buffer
adjustment on their sockets.

Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
arch/alpha/include/uapi/asm/socket.h
arch/mips/include/uapi/asm/socket.h
arch/parisc/include/uapi/asm/socket.h
arch/sparc/include/uapi/asm/socket.h
include/net/sock.h
include/uapi/asm-generic/socket.h
include/uapi/linux/socket.h
net/core/sock.c

index 6b3daba60987e4218ea316931017b1b6a23058bd..1dd9baf4a6c218b971f858e98a6512fa379a7063 100644 (file)
 
 #define SO_NETNS_COOKIE                71
 
+#define SO_BUF_LOCK            72
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
index cdf404a831b25f1bdf98b0ce9166a98cb42b1134..1eaf6a1ca56105aad156db0731833f5d085701aa 100644 (file)
 
 #define SO_NETNS_COOKIE                71
 
+#define SO_BUF_LOCK            72
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
index 5b5351cdcb3386de03aa4ad643fb5d4c939c40e3..8baaad52d79962db7c56a54fc27b6b0a6344be75 100644 (file)
 
 #define SO_NETNS_COOKIE                0x4045
 
+#define SO_BUF_LOCK            0x4046
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
index 92675dc380fa287097db3eeb89deaa64a2761280..e80ee8641ac3ea6d91939dec60bb121cbba6afc0 100644 (file)
 
 #define SO_NETNS_COOKIE          0x0050
 
+#define SO_BUF_LOCK              0x0051
+
 #if !defined(__KERNEL__)
 
 
index ff1be7e7e90bdc8da36ea214f8179132d98a8a55..6e761451c9278abc46a1c20a25d0c19e2886a222 100644 (file)
@@ -68,6 +68,7 @@
 #include <net/tcp_states.h>
 #include <linux/net_tstamp.h>
 #include <net/l3mdev.h>
+#include <uapi/linux/socket.h>
 
 /*
  * This structure really needs to be cleaned up.
@@ -1438,8 +1439,6 @@ static inline int __sk_prot_rehash(struct sock *sk)
 #define RCV_SHUTDOWN   1
 #define SEND_SHUTDOWN  2
 
-#define SOCK_SNDBUF_LOCK       1
-#define SOCK_RCVBUF_LOCK       2
 #define SOCK_BINDADDR_LOCK     4
 #define SOCK_BINDPORT_LOCK     8
 
index d588c244ec2faae1c3ee0ae131ce6e41c0276e36..1f0a2b4864e445f64e92aeb3345a192eb022da9f 100644 (file)
 
 #define SO_NETNS_COOKIE                71
 
+#define SO_BUF_LOCK            72
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
index c3409c8ec0ddd10de38c1a1e7742dff563ec8b71..eb0a9a5b6e71f3321fc55d5bd91407fb6bb43321 100644 (file)
@@ -26,4 +26,9 @@ struct __kernel_sockaddr_storage {
        };
 };
 
+#define SOCK_SNDBUF_LOCK       1
+#define SOCK_RCVBUF_LOCK       2
+
+#define SOCK_BUF_LOCK_MASK (SOCK_SNDBUF_LOCK | SOCK_RCVBUF_LOCK)
+
 #endif /* _UAPI_LINUX_SOCKET_H */
index 9671c32e6ef56d3e7fe9f132bc8f36bae4ca5ece..aada649e07e8dc8bb8f74f02c61f21704566cda1 100644 (file)
@@ -1358,6 +1358,15 @@ set_sndbuf:
                ret = sock_bindtoindex_locked(sk, val);
                break;
 
+       case SO_BUF_LOCK:
+               if (val & ~SOCK_BUF_LOCK_MASK) {
+                       ret = -EINVAL;
+                       break;
+               }
+               sk->sk_userlocks = val | (sk->sk_userlocks &
+                                         ~SOCK_BUF_LOCK_MASK);
+               break;
+
        default:
                ret = -ENOPROTOOPT;
                break;
@@ -1720,6 +1729,10 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
                v.val64 = sock_net(sk)->net_cookie;
                break;
 
+       case SO_BUF_LOCK:
+               v.val = sk->sk_userlocks & SOCK_BUF_LOCK_MASK;
+               break;
+
        default:
                /* We implement the SO_SNDLOWAT etc to not be settable
                 * (1003.1g 7).