@@ -789,6 +789,35 @@ void sock_set_keepalive(struct sock *sk)
789789}
790790EXPORT_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