Skip to content

Commit 26cfabf

Browse files
Christoph Hellwigdavem330
authored andcommitted
net: add sock_set_rcvbuf
Add a helper to directly set the SO_RCVBUFFORCE sockopt from kernel space without going through a fake uaccess. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ce3d954 commit 26cfabf

3 files changed

Lines changed: 34 additions & 33 deletions

File tree

‎fs/dlm/lowcomms.c‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,6 @@ static int sctp_listen_for_all(void)
11801180
struct socket *sock = NULL;
11811181
int result = -EINVAL;
11821182
struct connection *con = nodeid2con(0, GFP_NOFS);
1183-
int bufsize = NEEDED_RMEM;
11841183
int one = 1;
11851184

11861185
if (!con)
@@ -1195,11 +1194,7 @@ static int sctp_listen_for_all(void)
11951194
goto out;
11961195
}
11971196

1198-
result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
1199-
(char *)&bufsize, sizeof(bufsize));
1200-
if (result)
1201-
log_print("Error increasing buffer space on socket %d", result);
1202-
1197+
sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
12031198
result = kernel_setsockopt(sock, SOL_SCTP, SCTP_NODELAY, (char *)&one,
12041199
sizeof(one));
12051200
if (result < 0)

‎include/net/sock.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,6 +2693,7 @@ void sock_enable_timestamps(struct sock *sk);
26932693
void sock_no_linger(struct sock *sk);
26942694
void sock_set_keepalive(struct sock *sk);
26952695
void sock_set_priority(struct sock *sk, u32 priority);
2696+
void sock_set_rcvbuf(struct sock *sk, int val);
26962697
void sock_set_reuseaddr(struct sock *sk);
26972698
void sock_set_sndtimeo(struct sock *sk, s64 secs);
26982699

‎net/core/sock.c‎

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,35 @@ void sock_set_keepalive(struct sock *sk)
789789
}
790790
EXPORT_SYMBOL(sock_set_keepalive);
791791

792+
static void __sock_set_rcvbuf(struct sock *sk, int val)
793+
{
794+
/* Ensure val * 2 fits into an int, to prevent max_t() from treating it
795+
* as a negative value.
796+
*/
797+
val = min_t(int, val, INT_MAX / 2);
798+
sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
799+
800+
/* We double it on the way in to account for "struct sk_buff" etc.
801+
* overhead. Applications assume that the SO_RCVBUF setting they make
802+
* will allow that much actual data to be received on that socket.
803+
*
804+
* Applications are unaware that "struct sk_buff" and other overheads
805+
* allocate from the receive buffer during socket buffer allocation.
806+
*
807+
* And after considering the possible alternatives, returning the value
808+
* we actually used in getsockopt is the most desirable behavior.
809+
*/
810+
WRITE_ONCE(sk->sk_rcvbuf, max_t(int, val * 2, SOCK_MIN_RCVBUF));
811+
}
812+
813+
void sock_set_rcvbuf(struct sock *sk, int val)
814+
{
815+
lock_sock(sk);
816+
__sock_set_rcvbuf(sk, val);
817+
release_sock(sk);
818+
}
819+
EXPORT_SYMBOL(sock_set_rcvbuf);
820+
792821
/*
793822
* This is meant for all protocols to use and covers goings on
794823
* at the socket level. Everything here is generic.
@@ -885,30 +914,7 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
885914
* play 'guess the biggest size' games. RCVBUF/SNDBUF
886915
* are treated in BSD as hints
887916
*/
888-
val = min_t(u32, val, sysctl_rmem_max);
889-
set_rcvbuf:
890-
/* Ensure val * 2 fits into an int, to prevent max_t()
891-
* from treating it as a negative value.
892-
*/
893-
val = min_t(int, val, INT_MAX / 2);
894-
sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
895-
/*
896-
* We double it on the way in to account for
897-
* "struct sk_buff" etc. overhead. Applications
898-
* assume that the SO_RCVBUF setting they make will
899-
* allow that much actual data to be received on that
900-
* socket.
901-
*
902-
* Applications are unaware that "struct sk_buff" and
903-
* other overheads allocate from the receive buffer
904-
* during socket buffer allocation.
905-
*
906-
* And after considering the possible alternatives,
907-
* returning the value we actually used in getsockopt
908-
* is the most desirable behavior.
909-
*/
910-
WRITE_ONCE(sk->sk_rcvbuf,
911-
max_t(int, val * 2, SOCK_MIN_RCVBUF));
917+
__sock_set_rcvbuf(sk, min_t(u32, val, sysctl_rmem_max));
912918
break;
913919

914920
case SO_RCVBUFFORCE:
@@ -920,9 +926,8 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
920926
/* No negative values (to prevent underflow, as val will be
921927
* multiplied by 2).
922928
*/
923-
if (val < 0)
924-
val = 0;
925-
goto set_rcvbuf;
929+
__sock_set_rcvbuf(sk, max(val, 0));
930+
break;
926931

927932
case SO_KEEPALIVE:
928933
if (sk->sk_prot->keepalive)

0 commit comments

Comments
 (0)