Lines Matching refs:sk

149 static void sock_def_write_space_wfree(struct sock *sk);
150 static void sock_def_write_space(struct sock *sk);
162 bool sk_ns_capable(const struct sock *sk, in sk_ns_capable() argument
165 return file_ns_capable(sk->sk_socket->file, user_ns, cap) && in sk_ns_capable()
179 bool sk_capable(const struct sock *sk, int cap) in sk_capable() argument
181 return sk_ns_capable(sk, &init_user_ns, cap); in sk_capable()
194 bool sk_net_capable(const struct sock *sk, int cap) in sk_net_capable() argument
196 return sk_ns_capable(sk, sock_net(sk)->user_ns, cap); in sk_net_capable()
299 void sk_set_memalloc(struct sock *sk) in sk_set_memalloc() argument
301 sock_set_flag(sk, SOCK_MEMALLOC); in sk_set_memalloc()
302 sk->sk_allocation |= __GFP_MEMALLOC; in sk_set_memalloc()
307 void sk_clear_memalloc(struct sock *sk) in sk_clear_memalloc() argument
309 sock_reset_flag(sk, SOCK_MEMALLOC); in sk_clear_memalloc()
310 sk->sk_allocation &= ~__GFP_MEMALLOC; in sk_clear_memalloc()
320 sk_mem_reclaim(sk); in sk_clear_memalloc()
324 int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb) in __sk_backlog_rcv() argument
330 BUG_ON(!sock_flag(sk, SOCK_MEMALLOC)); in __sk_backlog_rcv()
333 ret = INDIRECT_CALL_INET(sk->sk_backlog_rcv, in __sk_backlog_rcv()
336 sk, skb); in __sk_backlog_rcv()
343 void sk_error_report(struct sock *sk) in sk_error_report() argument
345 sk->sk_error_report(sk); in sk_error_report()
347 switch (sk->sk_family) { in sk_error_report()
351 trace_inet_sk_error_report(sk); in sk_error_report()
454 static bool sock_needs_netstamp(const struct sock *sk) in sock_needs_netstamp() argument
456 switch (sk->sk_family) { in sock_needs_netstamp()
465 static void sock_disable_timestamp(struct sock *sk, unsigned long flags) in sock_disable_timestamp() argument
467 if (sk->sk_flags & flags) { in sock_disable_timestamp()
468 sk->sk_flags &= ~flags; in sock_disable_timestamp()
469 if (sock_needs_netstamp(sk) && in sock_disable_timestamp()
470 !(sk->sk_flags & SK_FLAGS_TIMESTAMP)) in sock_disable_timestamp()
476 int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) in __sock_queue_rcv_skb() argument
479 struct sk_buff_head *list = &sk->sk_receive_queue; in __sock_queue_rcv_skb()
481 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) { in __sock_queue_rcv_skb()
482 atomic_inc(&sk->sk_drops); in __sock_queue_rcv_skb()
483 trace_sock_rcvqueue_full(sk, skb); in __sock_queue_rcv_skb()
487 if (!sk_rmem_schedule(sk, skb, skb->truesize)) { in __sock_queue_rcv_skb()
488 atomic_inc(&sk->sk_drops); in __sock_queue_rcv_skb()
493 skb_set_owner_r(skb, sk); in __sock_queue_rcv_skb()
501 sock_skb_set_dropcount(sk, skb); in __sock_queue_rcv_skb()
505 if (!sock_flag(sk, SOCK_DEAD)) in __sock_queue_rcv_skb()
506 sk->sk_data_ready(sk); in __sock_queue_rcv_skb()
511 int sock_queue_rcv_skb_reason(struct sock *sk, struct sk_buff *skb, in sock_queue_rcv_skb_reason() argument
517 err = sk_filter(sk, skb); in sock_queue_rcv_skb_reason()
522 err = __sock_queue_rcv_skb(sk, skb); in sock_queue_rcv_skb_reason()
541 int __sk_receive_skb(struct sock *sk, struct sk_buff *skb, in __sk_receive_skb() argument
546 if (sk_filter_trim_cap(sk, skb, trim_cap)) in __sk_receive_skb()
551 if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) { in __sk_receive_skb()
552 atomic_inc(&sk->sk_drops); in __sk_receive_skb()
556 bh_lock_sock_nested(sk); in __sk_receive_skb()
558 bh_lock_sock(sk); in __sk_receive_skb()
559 if (!sock_owned_by_user(sk)) { in __sk_receive_skb()
563 mutex_acquire(&sk->sk_lock.dep_map, 0, 1, _RET_IP_); in __sk_receive_skb()
565 rc = sk_backlog_rcv(sk, skb); in __sk_receive_skb()
567 mutex_release(&sk->sk_lock.dep_map, _RET_IP_); in __sk_receive_skb()
568 } else if (sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf))) { in __sk_receive_skb()
569 bh_unlock_sock(sk); in __sk_receive_skb()
570 atomic_inc(&sk->sk_drops); in __sk_receive_skb()
574 bh_unlock_sock(sk); in __sk_receive_skb()
577 sock_put(sk); in __sk_receive_skb()
589 struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie) in __sk_dst_check() argument
591 struct dst_entry *dst = __sk_dst_get(sk); in __sk_dst_check()
596 sk_tx_queue_clear(sk); in __sk_dst_check()
597 sk->sk_dst_pending_confirm = 0; in __sk_dst_check()
598 RCU_INIT_POINTER(sk->sk_dst_cache, NULL); in __sk_dst_check()
607 struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie) in sk_dst_check() argument
609 struct dst_entry *dst = sk_dst_get(sk); in sk_dst_check()
614 sk_dst_reset(sk); in sk_dst_check()
623 static int sock_bindtoindex_locked(struct sock *sk, int ifindex) in sock_bindtoindex_locked() argument
627 struct net *net = sock_net(sk); in sock_bindtoindex_locked()
631 if (sk->sk_bound_dev_if && !ns_capable(net->user_ns, CAP_NET_RAW)) in sock_bindtoindex_locked()
639 WRITE_ONCE(sk->sk_bound_dev_if, ifindex); in sock_bindtoindex_locked()
641 if (sk->sk_prot->rehash) in sock_bindtoindex_locked()
642 sk->sk_prot->rehash(sk); in sock_bindtoindex_locked()
643 sk_dst_reset(sk); in sock_bindtoindex_locked()
653 int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk) in sock_bindtoindex() argument
658 lock_sock(sk); in sock_bindtoindex()
659 ret = sock_bindtoindex_locked(sk, ifindex); in sock_bindtoindex()
661 release_sock(sk); in sock_bindtoindex()
667 static int sock_setbindtodevice(struct sock *sk, sockptr_t optval, int optlen) in sock_setbindtodevice() argument
671 struct net *net = sock_net(sk); in sock_setbindtodevice()
706 sockopt_lock_sock(sk); in sock_setbindtodevice()
707 ret = sock_bindtoindex_locked(sk, index); in sock_setbindtodevice()
708 sockopt_release_sock(sk); in sock_setbindtodevice()
715 static int sock_getbindtodevice(struct sock *sk, sockptr_t optval, in sock_getbindtodevice() argument
720 int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); in sock_getbindtodevice()
721 struct net *net = sock_net(sk); in sock_getbindtodevice()
756 bool sk_mc_loop(struct sock *sk) in sk_mc_loop() argument
760 if (!sk) in sk_mc_loop()
762 switch (sk->sk_family) { in sk_mc_loop()
764 return inet_sk(sk)->mc_loop; in sk_mc_loop()
767 return inet6_sk(sk)->mc_loop; in sk_mc_loop()
775 void sock_set_reuseaddr(struct sock *sk) in sock_set_reuseaddr() argument
777 lock_sock(sk); in sock_set_reuseaddr()
778 sk->sk_reuse = SK_CAN_REUSE; in sock_set_reuseaddr()
779 release_sock(sk); in sock_set_reuseaddr()
783 void sock_set_reuseport(struct sock *sk) in sock_set_reuseport() argument
785 lock_sock(sk); in sock_set_reuseport()
786 sk->sk_reuseport = true; in sock_set_reuseport()
787 release_sock(sk); in sock_set_reuseport()
791 void sock_no_linger(struct sock *sk) in sock_no_linger() argument
793 lock_sock(sk); in sock_no_linger()
794 sk->sk_lingertime = 0; in sock_no_linger()
795 sock_set_flag(sk, SOCK_LINGER); in sock_no_linger()
796 release_sock(sk); in sock_no_linger()
800 void sock_set_priority(struct sock *sk, u32 priority) in sock_set_priority() argument
802 lock_sock(sk); in sock_set_priority()
803 sk->sk_priority = priority; in sock_set_priority()
804 release_sock(sk); in sock_set_priority()
808 void sock_set_sndtimeo(struct sock *sk, s64 secs) in sock_set_sndtimeo() argument
810 lock_sock(sk); in sock_set_sndtimeo()
812 sk->sk_sndtimeo = secs * HZ; in sock_set_sndtimeo()
814 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; in sock_set_sndtimeo()
815 release_sock(sk); in sock_set_sndtimeo()
819 static void __sock_set_timestamps(struct sock *sk, bool val, bool new, bool ns) in __sock_set_timestamps() argument
822 sock_valbool_flag(sk, SOCK_TSTAMP_NEW, new); in __sock_set_timestamps()
823 sock_valbool_flag(sk, SOCK_RCVTSTAMPNS, ns); in __sock_set_timestamps()
824 sock_set_flag(sk, SOCK_RCVTSTAMP); in __sock_set_timestamps()
825 sock_enable_timestamp(sk, SOCK_TIMESTAMP); in __sock_set_timestamps()
827 sock_reset_flag(sk, SOCK_RCVTSTAMP); in __sock_set_timestamps()
828 sock_reset_flag(sk, SOCK_RCVTSTAMPNS); in __sock_set_timestamps()
832 void sock_enable_timestamps(struct sock *sk) in sock_enable_timestamps() argument
834 lock_sock(sk); in sock_enable_timestamps()
835 __sock_set_timestamps(sk, true, false, true); in sock_enable_timestamps()
836 release_sock(sk); in sock_enable_timestamps()
840 void sock_set_timestamp(struct sock *sk, int optname, bool valbool) in sock_set_timestamp() argument
844 __sock_set_timestamps(sk, valbool, false, false); in sock_set_timestamp()
847 __sock_set_timestamps(sk, valbool, true, false); in sock_set_timestamp()
850 __sock_set_timestamps(sk, valbool, false, true); in sock_set_timestamp()
853 __sock_set_timestamps(sk, valbool, true, true); in sock_set_timestamp()
858 static int sock_timestamping_bind_phc(struct sock *sk, int phc_index) in sock_timestamping_bind_phc() argument
860 struct net *net = sock_net(sk); in sock_timestamping_bind_phc()
866 if (sk->sk_bound_dev_if) in sock_timestamping_bind_phc()
867 dev = dev_get_by_index(net, sk->sk_bound_dev_if); in sock_timestamping_bind_phc()
890 sk->sk_bind_phc = phc_index; in sock_timestamping_bind_phc()
895 int sock_set_timestamping(struct sock *sk, int optname, in sock_set_timestamping() argument
905 !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)) { in sock_set_timestamping()
906 if (sk_is_tcp(sk)) { in sock_set_timestamping()
907 if ((1 << sk->sk_state) & in sock_set_timestamping()
910 atomic_set(&sk->sk_tskey, tcp_sk(sk)->snd_una); in sock_set_timestamping()
912 atomic_set(&sk->sk_tskey, 0); in sock_set_timestamping()
921 ret = sock_timestamping_bind_phc(sk, timestamping.bind_phc); in sock_set_timestamping()
926 sk->sk_tsflags = val; in sock_set_timestamping()
927 sock_valbool_flag(sk, SOCK_TSTAMP_NEW, optname == SO_TIMESTAMPING_NEW); in sock_set_timestamping()
930 sock_enable_timestamp(sk, in sock_set_timestamping()
933 sock_disable_timestamp(sk, in sock_set_timestamping()
938 void sock_set_keepalive(struct sock *sk) in sock_set_keepalive() argument
940 lock_sock(sk); in sock_set_keepalive()
941 if (sk->sk_prot->keepalive) in sock_set_keepalive()
942 sk->sk_prot->keepalive(sk, true); in sock_set_keepalive()
943 sock_valbool_flag(sk, SOCK_KEEPOPEN, true); in sock_set_keepalive()
944 release_sock(sk); in sock_set_keepalive()
948 static void __sock_set_rcvbuf(struct sock *sk, int val) in __sock_set_rcvbuf() argument
954 sk->sk_userlocks |= SOCK_RCVBUF_LOCK; in __sock_set_rcvbuf()
966 WRITE_ONCE(sk->sk_rcvbuf, max_t(int, val * 2, SOCK_MIN_RCVBUF)); in __sock_set_rcvbuf()
969 void sock_set_rcvbuf(struct sock *sk, int val) in sock_set_rcvbuf() argument
971 lock_sock(sk); in sock_set_rcvbuf()
972 __sock_set_rcvbuf(sk, val); in sock_set_rcvbuf()
973 release_sock(sk); in sock_set_rcvbuf()
977 static void __sock_set_mark(struct sock *sk, u32 val) in __sock_set_mark() argument
979 if (val != sk->sk_mark) { in __sock_set_mark()
980 sk->sk_mark = val; in __sock_set_mark()
981 sk_dst_reset(sk); in __sock_set_mark()
985 void sock_set_mark(struct sock *sk, u32 val) in sock_set_mark() argument
987 lock_sock(sk); in sock_set_mark()
988 __sock_set_mark(sk, val); in sock_set_mark()
989 release_sock(sk); in sock_set_mark()
993 static void sock_release_reserved_memory(struct sock *sk, int bytes) in sock_release_reserved_memory() argument
998 WARN_ON(bytes > sk->sk_reserved_mem); in sock_release_reserved_memory()
999 sk->sk_reserved_mem -= bytes; in sock_release_reserved_memory()
1000 sk_mem_reclaim(sk); in sock_release_reserved_memory()
1003 static int sock_reserve_memory(struct sock *sk, int bytes) in sock_reserve_memory() argument
1009 if (!mem_cgroup_sockets_enabled || !sk->sk_memcg || !sk_has_account(sk)) in sock_reserve_memory()
1018 charged = mem_cgroup_charge_skmem(sk->sk_memcg, pages, in sock_reserve_memory()
1024 sk_memory_allocated_add(sk, pages); in sock_reserve_memory()
1025 allocated = sk_memory_allocated(sk); in sock_reserve_memory()
1029 if (allocated > sk_prot_mem_limits(sk, 1)) { in sock_reserve_memory()
1030 sk_memory_allocated_sub(sk, pages); in sock_reserve_memory()
1031 mem_cgroup_uncharge_skmem(sk->sk_memcg, pages); in sock_reserve_memory()
1034 sk->sk_forward_alloc += pages << PAGE_SHIFT; in sock_reserve_memory()
1036 sk->sk_reserved_mem += pages << PAGE_SHIFT; in sock_reserve_memory()
1041 void sockopt_lock_sock(struct sock *sk) in sockopt_lock_sock() argument
1050 lock_sock(sk); in sockopt_lock_sock()
1054 void sockopt_release_sock(struct sock *sk) in sockopt_release_sock() argument
1059 release_sock(sk); in sockopt_release_sock()
1080 int sk_setsockopt(struct sock *sk, int level, int optname, in sk_setsockopt() argument
1084 struct socket *sock = sk->sk_socket; in sk_setsockopt()
1096 return sock_setbindtodevice(sk, optval, optlen); in sk_setsockopt()
1106 sockopt_lock_sock(sk); in sk_setsockopt()
1113 sock_valbool_flag(sk, SOCK_DBG, valbool); in sk_setsockopt()
1116 sk->sk_reuse = (valbool ? SK_CAN_REUSE : SK_NO_REUSE); in sk_setsockopt()
1119 sk->sk_reuseport = valbool; in sk_setsockopt()
1128 sock_valbool_flag(sk, SOCK_LOCALROUTE, valbool); in sk_setsockopt()
1129 sk_dst_reset(sk); in sk_setsockopt()
1132 sock_valbool_flag(sk, SOCK_BROADCAST, valbool); in sk_setsockopt()
1146 sk->sk_userlocks |= SOCK_SNDBUF_LOCK; in sk_setsockopt()
1147 WRITE_ONCE(sk->sk_sndbuf, in sk_setsockopt()
1150 sk->sk_write_space(sk); in sk_setsockopt()
1172 __sock_set_rcvbuf(sk, min_t(u32, val, READ_ONCE(sysctl_rmem_max))); in sk_setsockopt()
1184 __sock_set_rcvbuf(sk, max(val, 0)); in sk_setsockopt()
1188 if (sk->sk_prot->keepalive) in sk_setsockopt()
1189 sk->sk_prot->keepalive(sk, valbool); in sk_setsockopt()
1190 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool); in sk_setsockopt()
1194 sock_valbool_flag(sk, SOCK_URGINLINE, valbool); in sk_setsockopt()
1198 sk->sk_no_check_tx = valbool; in sk_setsockopt()
1203 sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) || in sk_setsockopt()
1204 sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) in sk_setsockopt()
1205 sk->sk_priority = val; in sk_setsockopt()
1220 sock_reset_flag(sk, SOCK_LINGER); in sk_setsockopt()
1224 sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT; in sk_setsockopt()
1227 sk->sk_lingertime = (unsigned int)ling.l_linger * HZ; in sk_setsockopt()
1228 sock_set_flag(sk, SOCK_LINGER); in sk_setsockopt()
1246 sock_set_timestamp(sk, optname, valbool); in sk_setsockopt()
1261 ret = sock_set_timestamping(sk, optname, timestamping); in sk_setsockopt()
1268 ret = sock->ops->set_rcvlowat(sk, val); in sk_setsockopt()
1270 WRITE_ONCE(sk->sk_rcvlowat, val ? : 1); in sk_setsockopt()
1275 ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, in sk_setsockopt()
1281 ret = sock_set_timeout(&sk->sk_sndtimeo, optval, in sk_setsockopt()
1290 ret = sk_attach_filter(&fprog, sk); in sk_setsockopt()
1302 ret = sk_attach_bpf(ufd, sk); in sk_setsockopt()
1311 ret = sk_reuseport_attach_filter(&fprog, sk); in sk_setsockopt()
1323 ret = sk_reuseport_attach_bpf(ufd, sk); in sk_setsockopt()
1328 ret = reuseport_detach_prog(sk); in sk_setsockopt()
1332 ret = sk_detach_filter(sk); in sk_setsockopt()
1336 if (sock_flag(sk, SOCK_FILTER_LOCKED) && !valbool) in sk_setsockopt()
1339 sock_valbool_flag(sk, SOCK_FILTER_LOCKED, valbool); in sk_setsockopt()
1349 if (!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) && in sk_setsockopt()
1350 !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) { in sk_setsockopt()
1355 __sock_set_mark(sk, val); in sk_setsockopt()
1358 if (!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) && in sk_setsockopt()
1359 !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) { in sk_setsockopt()
1364 sock_valbool_flag(sk, SOCK_RCVMARK, valbool); in sk_setsockopt()
1368 sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool); in sk_setsockopt()
1372 sock_valbool_flag(sk, SOCK_WIFI_STATUS, valbool); in sk_setsockopt()
1377 ret = sock->ops->set_peek_off(sk, val); in sk_setsockopt()
1383 sock_valbool_flag(sk, SOCK_NOFCS, valbool); in sk_setsockopt()
1387 sock_valbool_flag(sk, SOCK_SELECT_ERR_QUEUE, valbool); in sk_setsockopt()
1393 if ((val > sk->sk_ll_usec) && !sockopt_capable(CAP_NET_ADMIN)) in sk_setsockopt()
1399 WRITE_ONCE(sk->sk_ll_usec, val); in sk_setsockopt()
1406 WRITE_ONCE(sk->sk_prefer_busy_poll, valbool); in sk_setsockopt()
1409 if (val > READ_ONCE(sk->sk_busy_poll_budget) && !sockopt_capable(CAP_NET_ADMIN)) { in sk_setsockopt()
1415 WRITE_ONCE(sk->sk_busy_poll_budget, val); in sk_setsockopt()
1431 cmpxchg(&sk->sk_pacing_status, in sk_setsockopt()
1434 sk->sk_max_pacing_rate = ulval; in sk_setsockopt()
1435 sk->sk_pacing_rate = min(sk->sk_pacing_rate, ulval); in sk_setsockopt()
1439 reuseport_update_incoming_cpu(sk, val); in sk_setsockopt()
1444 dst_negative_advice(sk); in sk_setsockopt()
1448 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6) { in sk_setsockopt()
1449 if (!(sk_is_tcp(sk) || in sk_setsockopt()
1450 (sk->sk_type == SOCK_DGRAM && in sk_setsockopt()
1451 sk->sk_protocol == IPPROTO_UDP))) in sk_setsockopt()
1453 } else if (sk->sk_family != PF_RDS) { in sk_setsockopt()
1460 sock_valbool_flag(sk, SOCK_ZEROCOPY, valbool); in sk_setsockopt()
1480 !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) { in sk_setsockopt()
1484 sock_valbool_flag(sk, SOCK_TXTIME, true); in sk_setsockopt()
1485 sk->sk_clockid = sk_txtime.clockid; in sk_setsockopt()
1486 sk->sk_txtime_deadline_mode = in sk_setsockopt()
1488 sk->sk_txtime_report_errors = in sk_setsockopt()
1493 ret = sock_bindtoindex_locked(sk, val); in sk_setsockopt()
1501 sk->sk_userlocks = val | (sk->sk_userlocks & in sk_setsockopt()
1514 delta = val - sk->sk_reserved_mem; in sk_setsockopt()
1516 sock_release_reserved_memory(sk, -delta); in sk_setsockopt()
1518 ret = sock_reserve_memory(sk, delta); in sk_setsockopt()
1528 WRITE_ONCE(sk->sk_txrehash, (u8)val); in sk_setsockopt()
1535 sockopt_release_sock(sk); in sk_setsockopt()
1542 return sk_setsockopt(sock->sk, level, optname, in sock_setsockopt()
1547 static const struct cred *sk_get_peer_cred(struct sock *sk) in sk_get_peer_cred() argument
1551 spin_lock(&sk->sk_peer_lock); in sk_get_peer_cred()
1552 cred = get_cred(sk->sk_peer_cred); in sk_get_peer_cred()
1553 spin_unlock(&sk->sk_peer_lock); in sk_get_peer_cred()
1586 int sk_getsockopt(struct sock *sk, int level, int optname, in sk_getsockopt() argument
1589 struct socket *sock = sk->sk_socket; in sk_getsockopt()
1615 v.val = sock_flag(sk, SOCK_DBG); in sk_getsockopt()
1619 v.val = sock_flag(sk, SOCK_LOCALROUTE); in sk_getsockopt()
1623 v.val = sock_flag(sk, SOCK_BROADCAST); in sk_getsockopt()
1627 v.val = sk->sk_sndbuf; in sk_getsockopt()
1631 v.val = sk->sk_rcvbuf; in sk_getsockopt()
1635 v.val = sk->sk_reuse; in sk_getsockopt()
1639 v.val = sk->sk_reuseport; in sk_getsockopt()
1643 v.val = sock_flag(sk, SOCK_KEEPOPEN); in sk_getsockopt()
1647 v.val = sk->sk_type; in sk_getsockopt()
1651 v.val = sk->sk_protocol; in sk_getsockopt()
1655 v.val = sk->sk_family; in sk_getsockopt()
1659 v.val = -sock_error(sk); in sk_getsockopt()
1661 v.val = xchg(&sk->sk_err_soft, 0); in sk_getsockopt()
1665 v.val = sock_flag(sk, SOCK_URGINLINE); in sk_getsockopt()
1669 v.val = sk->sk_no_check_tx; in sk_getsockopt()
1673 v.val = sk->sk_priority; in sk_getsockopt()
1678 v.ling.l_onoff = sock_flag(sk, SOCK_LINGER); in sk_getsockopt()
1679 v.ling.l_linger = sk->sk_lingertime / HZ; in sk_getsockopt()
1686 v.val = sock_flag(sk, SOCK_RCVTSTAMP) && in sk_getsockopt()
1687 !sock_flag(sk, SOCK_TSTAMP_NEW) && in sk_getsockopt()
1688 !sock_flag(sk, SOCK_RCVTSTAMPNS); in sk_getsockopt()
1692 v.val = sock_flag(sk, SOCK_RCVTSTAMPNS) && !sock_flag(sk, SOCK_TSTAMP_NEW); in sk_getsockopt()
1696 v.val = sock_flag(sk, SOCK_RCVTSTAMP) && sock_flag(sk, SOCK_TSTAMP_NEW); in sk_getsockopt()
1700 v.val = sock_flag(sk, SOCK_RCVTSTAMPNS) && sock_flag(sk, SOCK_TSTAMP_NEW); in sk_getsockopt()
1705 v.timestamping.flags = sk->sk_tsflags; in sk_getsockopt()
1706 v.timestamping.bind_phc = sk->sk_bind_phc; in sk_getsockopt()
1711 lv = sock_get_timeout(sk->sk_rcvtimeo, &v, SO_RCVTIMEO_OLD == optname); in sk_getsockopt()
1716 lv = sock_get_timeout(sk->sk_sndtimeo, &v, SO_SNDTIMEO_OLD == optname); in sk_getsockopt()
1720 v.val = sk->sk_rcvlowat; in sk_getsockopt()
1737 spin_lock(&sk->sk_peer_lock); in sk_getsockopt()
1738 cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred); in sk_getsockopt()
1739 spin_unlock(&sk->sk_peer_lock); in sk_getsockopt()
1751 cred = sk_get_peer_cred(sk); in sk_getsockopt()
1788 v.val = sk->sk_state == TCP_LISTEN; in sk_getsockopt()
1799 v.val = sk->sk_mark; in sk_getsockopt()
1803 v.val = sock_flag(sk, SOCK_RCVMARK); in sk_getsockopt()
1807 v.val = sock_flag(sk, SOCK_RXQ_OVFL); in sk_getsockopt()
1811 v.val = sock_flag(sk, SOCK_WIFI_STATUS); in sk_getsockopt()
1818 v.val = sk->sk_peek_off; in sk_getsockopt()
1821 v.val = sock_flag(sk, SOCK_NOFCS); in sk_getsockopt()
1825 return sock_getbindtodevice(sk, optval, optlen, len); in sk_getsockopt()
1828 len = sk_get_filter(sk, optval, len); in sk_getsockopt()
1835 v.val = sock_flag(sk, SOCK_FILTER_LOCKED); in sk_getsockopt()
1843 v.val = sock_flag(sk, SOCK_SELECT_ERR_QUEUE); in sk_getsockopt()
1848 v.val = sk->sk_ll_usec; in sk_getsockopt()
1851 v.val = READ_ONCE(sk->sk_prefer_busy_poll); in sk_getsockopt()
1858 v.ulval = sk->sk_max_pacing_rate; in sk_getsockopt()
1861 v.val = min_t(unsigned long, sk->sk_max_pacing_rate, ~0U); in sk_getsockopt()
1866 v.val = READ_ONCE(sk->sk_incoming_cpu); in sk_getsockopt()
1873 sk_get_meminfo(sk, meminfo); in sk_getsockopt()
1884 v.val = READ_ONCE(sk->sk_napi_id); in sk_getsockopt()
1897 v.val64 = sock_gen_cookie(sk); in sk_getsockopt()
1901 v.val = sock_flag(sk, SOCK_ZEROCOPY); in sk_getsockopt()
1906 v.txtime.clockid = sk->sk_clockid; in sk_getsockopt()
1907 v.txtime.flags |= sk->sk_txtime_deadline_mode ? in sk_getsockopt()
1909 v.txtime.flags |= sk->sk_txtime_report_errors ? in sk_getsockopt()
1914 v.val = READ_ONCE(sk->sk_bound_dev_if); in sk_getsockopt()
1921 v.val64 = sock_net(sk)->net_cookie; in sk_getsockopt()
1925 v.val = sk->sk_userlocks & SOCK_BUF_LOCK_MASK; in sk_getsockopt()
1929 v.val = sk->sk_reserved_mem; in sk_getsockopt()
1933 v.val = sk->sk_txrehash; in sk_getsockopt()
1956 return sk_getsockopt(sock->sk, level, optname, in sock_getsockopt()
1966 static inline void sock_lock_init(struct sock *sk) in sock_lock_init() argument
1968 if (sk->sk_kern_sock) in sock_lock_init()
1970 sk, in sock_lock_init()
1971 af_family_kern_slock_key_strings[sk->sk_family], in sock_lock_init()
1972 af_family_kern_slock_keys + sk->sk_family, in sock_lock_init()
1973 af_family_kern_key_strings[sk->sk_family], in sock_lock_init()
1974 af_family_kern_keys + sk->sk_family); in sock_lock_init()
1977 sk, in sock_lock_init()
1978 af_family_slock_key_strings[sk->sk_family], in sock_lock_init()
1979 af_family_slock_keys + sk->sk_family, in sock_lock_init()
1980 af_family_key_strings[sk->sk_family], in sock_lock_init()
1981 af_family_keys + sk->sk_family); in sock_lock_init()
2019 struct sock *sk; in sk_prot_alloc() local
2024 sk = kmem_cache_alloc(slab, priority & ~__GFP_ZERO); in sk_prot_alloc()
2025 if (!sk) in sk_prot_alloc()
2026 return sk; in sk_prot_alloc()
2028 sk_prot_clear_nulls(sk, prot->obj_size); in sk_prot_alloc()
2030 sk = kmalloc(prot->obj_size, priority); in sk_prot_alloc()
2032 if (sk != NULL) { in sk_prot_alloc()
2033 if (security_sk_alloc(sk, family, priority)) in sk_prot_alloc()
2040 return sk; in sk_prot_alloc()
2043 security_sk_free(sk); in sk_prot_alloc()
2046 kmem_cache_free(slab, sk); in sk_prot_alloc()
2048 kfree(sk); in sk_prot_alloc()
2052 static void sk_prot_free(struct proto *prot, struct sock *sk) in sk_prot_free() argument
2060 cgroup_sk_free(&sk->sk_cgrp_data); in sk_prot_free()
2061 mem_cgroup_sk_free(sk); in sk_prot_free()
2062 security_sk_free(sk); in sk_prot_free()
2064 kmem_cache_free(slab, sk); in sk_prot_free()
2066 kfree(sk); in sk_prot_free()
2081 struct sock *sk; in sk_alloc() local
2083 sk = sk_prot_alloc(prot, priority | __GFP_ZERO, family); in sk_alloc()
2084 if (sk) { in sk_alloc()
2085 sk->sk_family = family; in sk_alloc()
2090 sk->sk_prot = sk->sk_prot_creator = prot; in sk_alloc()
2091 sk->sk_kern_sock = kern; in sk_alloc()
2092 sock_lock_init(sk); in sk_alloc()
2093 sk->sk_net_refcnt = kern ? 0 : 1; in sk_alloc()
2094 if (likely(sk->sk_net_refcnt)) { in sk_alloc()
2095 get_net_track(net, &sk->ns_tracker, priority); in sk_alloc()
2099 sock_net_set(sk, net); in sk_alloc()
2100 refcount_set(&sk->sk_wmem_alloc, 1); in sk_alloc()
2102 mem_cgroup_sk_alloc(sk); in sk_alloc()
2103 cgroup_sk_alloc(&sk->sk_cgrp_data); in sk_alloc()
2104 sock_update_classid(&sk->sk_cgrp_data); in sk_alloc()
2105 sock_update_netprioidx(&sk->sk_cgrp_data); in sk_alloc()
2106 sk_tx_queue_clear(sk); in sk_alloc()
2109 return sk; in sk_alloc()
2118 struct sock *sk = container_of(head, struct sock, sk_rcu); in __sk_destruct() local
2121 if (sk->sk_destruct) in __sk_destruct()
2122 sk->sk_destruct(sk); in __sk_destruct()
2124 filter = rcu_dereference_check(sk->sk_filter, in __sk_destruct()
2125 refcount_read(&sk->sk_wmem_alloc) == 0); in __sk_destruct()
2127 sk_filter_uncharge(sk, filter); in __sk_destruct()
2128 RCU_INIT_POINTER(sk->sk_filter, NULL); in __sk_destruct()
2131 sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP); in __sk_destruct()
2134 bpf_sk_storage_free(sk); in __sk_destruct()
2137 if (atomic_read(&sk->sk_omem_alloc)) in __sk_destruct()
2139 __func__, atomic_read(&sk->sk_omem_alloc)); in __sk_destruct()
2141 if (sk->sk_frag.page) { in __sk_destruct()
2142 put_page(sk->sk_frag.page); in __sk_destruct()
2143 sk->sk_frag.page = NULL; in __sk_destruct()
2147 put_cred(sk->sk_peer_cred); in __sk_destruct()
2148 put_pid(sk->sk_peer_pid); in __sk_destruct()
2150 if (likely(sk->sk_net_refcnt)) in __sk_destruct()
2151 put_net_track(sock_net(sk), &sk->ns_tracker); in __sk_destruct()
2152 sk_prot_free(sk->sk_prot_creator, sk); in __sk_destruct()
2155 void sk_destruct(struct sock *sk) in sk_destruct() argument
2157 bool use_call_rcu = sock_flag(sk, SOCK_RCU_FREE); in sk_destruct()
2159 if (rcu_access_pointer(sk->sk_reuseport_cb)) { in sk_destruct()
2160 reuseport_detach_sock(sk); in sk_destruct()
2165 call_rcu(&sk->sk_rcu, __sk_destruct); in sk_destruct()
2167 __sk_destruct(&sk->sk_rcu); in sk_destruct()
2170 static void __sk_free(struct sock *sk) in __sk_free() argument
2172 if (likely(sk->sk_net_refcnt)) in __sk_free()
2173 sock_inuse_add(sock_net(sk), -1); in __sk_free()
2175 if (unlikely(sk->sk_net_refcnt && sock_diag_has_destroy_listeners(sk))) in __sk_free()
2176 sock_diag_broadcast_destroy(sk); in __sk_free()
2178 sk_destruct(sk); in __sk_free()
2181 void sk_free(struct sock *sk) in sk_free() argument
2188 if (refcount_dec_and_test(&sk->sk_wmem_alloc)) in sk_free()
2189 __sk_free(sk); in sk_free()
2193 static void sk_init_common(struct sock *sk) in sk_init_common() argument
2195 skb_queue_head_init(&sk->sk_receive_queue); in sk_init_common()
2196 skb_queue_head_init(&sk->sk_write_queue); in sk_init_common()
2197 skb_queue_head_init(&sk->sk_error_queue); in sk_init_common()
2199 rwlock_init(&sk->sk_callback_lock); in sk_init_common()
2200 lockdep_set_class_and_name(&sk->sk_receive_queue.lock, in sk_init_common()
2201 af_rlock_keys + sk->sk_family, in sk_init_common()
2202 af_family_rlock_key_strings[sk->sk_family]); in sk_init_common()
2203 lockdep_set_class_and_name(&sk->sk_write_queue.lock, in sk_init_common()
2204 af_wlock_keys + sk->sk_family, in sk_init_common()
2205 af_family_wlock_key_strings[sk->sk_family]); in sk_init_common()
2206 lockdep_set_class_and_name(&sk->sk_error_queue.lock, in sk_init_common()
2207 af_elock_keys + sk->sk_family, in sk_init_common()
2208 af_family_elock_key_strings[sk->sk_family]); in sk_init_common()
2209 lockdep_set_class_and_name(&sk->sk_callback_lock, in sk_init_common()
2210 af_callback_keys + sk->sk_family, in sk_init_common()
2211 af_family_clock_key_strings[sk->sk_family]); in sk_init_common()
2221 struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) in sk_clone_lock() argument
2223 struct proto *prot = READ_ONCE(sk->sk_prot); in sk_clone_lock()
2228 newsk = sk_prot_alloc(prot, priority, sk->sk_family); in sk_clone_lock()
2232 sock_copy(newsk, sk); in sk_clone_lock()
2262 newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK; in sk_clone_lock()
2273 filter = rcu_dereference(sk->sk_filter); in sk_clone_lock()
2283 if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) { in sk_clone_lock()
2296 if (bpf_sk_storage_clone(sk, newsk)) { in sk_clone_lock()
2337 if (sock_needs_netstamp(sk) && newsk->sk_flags & SK_FLAGS_TIMESTAMP) in sk_clone_lock()
2344 void sk_free_unlock_clone(struct sock *sk) in sk_free_unlock_clone() argument
2348 sk->sk_destruct = NULL; in sk_free_unlock_clone()
2349 bh_unlock_sock(sk); in sk_free_unlock_clone()
2350 sk_free(sk); in sk_free_unlock_clone()
2354 static void sk_trim_gso_size(struct sock *sk) in sk_trim_gso_size() argument
2356 if (sk->sk_gso_max_size <= GSO_LEGACY_MAX_SIZE) in sk_trim_gso_size()
2359 if (sk->sk_family == AF_INET6 && in sk_trim_gso_size()
2360 sk_is_tcp(sk) && in sk_trim_gso_size()
2361 !ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr)) in sk_trim_gso_size()
2364 sk->sk_gso_max_size = GSO_LEGACY_MAX_SIZE; in sk_trim_gso_size()
2367 void sk_setup_caps(struct sock *sk, struct dst_entry *dst) in sk_setup_caps() argument
2371 sk_dst_set(sk, dst); in sk_setup_caps()
2372 sk->sk_route_caps = dst->dev->features; in sk_setup_caps()
2373 if (sk_is_tcp(sk)) in sk_setup_caps()
2374 sk->sk_route_caps |= NETIF_F_GSO; in sk_setup_caps()
2375 if (sk->sk_route_caps & NETIF_F_GSO) in sk_setup_caps()
2376 sk->sk_route_caps |= NETIF_F_GSO_SOFTWARE; in sk_setup_caps()
2377 if (unlikely(sk->sk_gso_disabled)) in sk_setup_caps()
2378 sk->sk_route_caps &= ~NETIF_F_GSO_MASK; in sk_setup_caps()
2379 if (sk_can_gso(sk)) { in sk_setup_caps()
2381 sk->sk_route_caps &= ~NETIF_F_GSO_MASK; in sk_setup_caps()
2383 sk->sk_route_caps |= NETIF_F_SG | NETIF_F_HW_CSUM; in sk_setup_caps()
2385 sk->sk_gso_max_size = READ_ONCE(dst->dev->gso_max_size); in sk_setup_caps()
2386 sk_trim_gso_size(sk); in sk_setup_caps()
2387 sk->sk_gso_max_size -= (MAX_TCP_HEADER + 1); in sk_setup_caps()
2392 sk->sk_gso_max_segs = max_segs; in sk_setup_caps()
2406 struct sock *sk = skb->sk; in sock_wfree() local
2410 if (!sock_flag(sk, SOCK_USE_WRITE_QUEUE)) { in sock_wfree()
2411 if (sock_flag(sk, SOCK_RCU_FREE) && in sock_wfree()
2412 sk->sk_write_space == sock_def_write_space) { in sock_wfree()
2414 free = refcount_sub_and_test(len, &sk->sk_wmem_alloc); in sock_wfree()
2415 sock_def_write_space_wfree(sk); in sock_wfree()
2418 __sk_free(sk); in sock_wfree()
2426 WARN_ON(refcount_sub_and_test(len - 1, &sk->sk_wmem_alloc)); in sock_wfree()
2427 sk->sk_write_space(sk); in sock_wfree()
2434 if (refcount_sub_and_test(len, &sk->sk_wmem_alloc)) in sock_wfree()
2435 __sk_free(sk); in sock_wfree()
2444 struct sock *sk = skb->sk; in __sock_wfree() local
2446 if (refcount_sub_and_test(skb->truesize, &sk->sk_wmem_alloc)) in __sock_wfree()
2447 __sk_free(sk); in __sock_wfree()
2450 void skb_set_owner_w(struct sk_buff *skb, struct sock *sk) in skb_set_owner_w() argument
2453 skb->sk = sk; in skb_set_owner_w()
2455 if (unlikely(!sk_fullsock(sk))) { in skb_set_owner_w()
2457 sock_hold(sk); in skb_set_owner_w()
2462 skb_set_hash_from_sk(skb, sk); in skb_set_owner_w()
2468 refcount_add(skb->truesize, &sk->sk_wmem_alloc); in skb_set_owner_w()
2496 if (can_skb_orphan_partial(skb) && skb_set_owner_sk_safe(skb, skb->sk)) in skb_orphan_partial()
2508 struct sock *sk = skb->sk; in sock_rfree() local
2511 atomic_sub(len, &sk->sk_rmem_alloc); in sock_rfree()
2512 sk_mem_uncharge(sk, len); in sock_rfree()
2522 sock_put(skb->sk); in sock_efree()
2532 if (sk_is_refcounted(skb->sk)) in sock_pfree()
2533 sock_gen_put(skb->sk); in sock_pfree()
2538 kuid_t sock_i_uid(struct sock *sk) in sock_i_uid() argument
2542 read_lock_bh(&sk->sk_callback_lock); in sock_i_uid()
2543 uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : GLOBAL_ROOT_UID; in sock_i_uid()
2544 read_unlock_bh(&sk->sk_callback_lock); in sock_i_uid()
2549 unsigned long sock_i_ino(struct sock *sk) in sock_i_ino() argument
2553 read_lock_bh(&sk->sk_callback_lock); in sock_i_ino()
2554 ino = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_ino : 0; in sock_i_ino()
2555 read_unlock_bh(&sk->sk_callback_lock); in sock_i_ino()
2563 struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force, in sock_wmalloc() argument
2567 refcount_read(&sk->sk_wmem_alloc) < READ_ONCE(sk->sk_sndbuf)) { in sock_wmalloc()
2571 skb_set_owner_w(skb, sk); in sock_wmalloc()
2581 struct sock *sk = skb->sk; in sock_ofree() local
2583 atomic_sub(skb->truesize, &sk->sk_omem_alloc); in sock_ofree()
2586 struct sk_buff *sock_omalloc(struct sock *sk, unsigned long size, in sock_omalloc() argument
2592 if (atomic_read(&sk->sk_omem_alloc) + SKB_TRUESIZE(size) > in sock_omalloc()
2600 atomic_add(skb->truesize, &sk->sk_omem_alloc); in sock_omalloc()
2601 skb->sk = sk; in sock_omalloc()
2609 void *sock_kmalloc(struct sock *sk, int size, gfp_t priority) in sock_kmalloc() argument
2614 atomic_read(&sk->sk_omem_alloc) + size < optmem_max) { in sock_kmalloc()
2619 atomic_add(size, &sk->sk_omem_alloc); in sock_kmalloc()
2623 atomic_sub(size, &sk->sk_omem_alloc); in sock_kmalloc()
2633 static inline void __sock_kfree_s(struct sock *sk, void *mem, int size, in __sock_kfree_s() argument
2642 atomic_sub(size, &sk->sk_omem_alloc); in __sock_kfree_s()
2645 void sock_kfree_s(struct sock *sk, void *mem, int size) in sock_kfree_s() argument
2647 __sock_kfree_s(sk, mem, size, false); in sock_kfree_s()
2651 void sock_kzfree_s(struct sock *sk, void *mem, int size) in sock_kzfree_s() argument
2653 __sock_kfree_s(sk, mem, size, true); in sock_kzfree_s()
2660 static long sock_wait_for_wmem(struct sock *sk, long timeo) in sock_wait_for_wmem() argument
2664 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); in sock_wait_for_wmem()
2670 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); in sock_wait_for_wmem()
2671 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); in sock_wait_for_wmem()
2672 if (refcount_read(&sk->sk_wmem_alloc) < READ_ONCE(sk->sk_sndbuf)) in sock_wait_for_wmem()
2674 if (sk->sk_shutdown & SEND_SHUTDOWN) in sock_wait_for_wmem()
2676 if (sk->sk_err) in sock_wait_for_wmem()
2680 finish_wait(sk_sleep(sk), &wait); in sock_wait_for_wmem()
2689 struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len, in sock_alloc_send_pskb() argument
2697 timeo = sock_sndtimeo(sk, noblock); in sock_alloc_send_pskb()
2699 err = sock_error(sk); in sock_alloc_send_pskb()
2704 if (sk->sk_shutdown & SEND_SHUTDOWN) in sock_alloc_send_pskb()
2707 if (sk_wmem_alloc_get(sk) < READ_ONCE(sk->sk_sndbuf)) in sock_alloc_send_pskb()
2710 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); in sock_alloc_send_pskb()
2711 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); in sock_alloc_send_pskb()
2717 timeo = sock_wait_for_wmem(sk, timeo); in sock_alloc_send_pskb()
2720 errcode, sk->sk_allocation); in sock_alloc_send_pskb()
2722 skb_set_owner_w(skb, sk); in sock_alloc_send_pskb()
2733 int __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg, in __sock_cmsg_send() argument
2740 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) && in __sock_cmsg_send()
2741 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) in __sock_cmsg_send()
2759 if (!sock_flag(sk, SOCK_TXTIME)) in __sock_cmsg_send()
2776 int sock_cmsg_send(struct sock *sk, struct msghdr *msg, in sock_cmsg_send() argument
2787 ret = __sock_cmsg_send(sk, msg, cmsg, sockc); in sock_cmsg_send()
2795 static void sk_enter_memory_pressure(struct sock *sk) in sk_enter_memory_pressure() argument
2797 if (!sk->sk_prot->enter_memory_pressure) in sk_enter_memory_pressure()
2800 sk->sk_prot->enter_memory_pressure(sk); in sk_enter_memory_pressure()
2803 static void sk_leave_memory_pressure(struct sock *sk) in sk_leave_memory_pressure() argument
2805 if (sk->sk_prot->leave_memory_pressure) { in sk_leave_memory_pressure()
2806 sk->sk_prot->leave_memory_pressure(sk); in sk_leave_memory_pressure()
2808 unsigned long *memory_pressure = sk->sk_prot->memory_pressure; in sk_leave_memory_pressure()
2861 bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag) in sk_page_frag_refill() argument
2863 if (likely(skb_page_frag_refill(32U, pfrag, sk->sk_allocation))) in sk_page_frag_refill()
2866 sk_enter_memory_pressure(sk); in sk_page_frag_refill()
2867 sk_stream_moderate_sndbuf(sk); in sk_page_frag_refill()
2872 void __lock_sock(struct sock *sk) in __lock_sock() argument
2873 __releases(&sk->sk_lock.slock) in __lock_sock()
2874 __acquires(&sk->sk_lock.slock) in __lock_sock()
2879 prepare_to_wait_exclusive(&sk->sk_lock.wq, &wait, in __lock_sock()
2881 spin_unlock_bh(&sk->sk_lock.slock); in __lock_sock()
2883 spin_lock_bh(&sk->sk_lock.slock); in __lock_sock()
2884 if (!sock_owned_by_user(sk)) in __lock_sock()
2887 finish_wait(&sk->sk_lock.wq, &wait); in __lock_sock()
2890 void __release_sock(struct sock *sk) in __release_sock() argument
2891 __releases(&sk->sk_lock.slock) in __release_sock()
2892 __acquires(&sk->sk_lock.slock) in __release_sock()
2896 while ((skb = sk->sk_backlog.head) != NULL) { in __release_sock()
2897 sk->sk_backlog.head = sk->sk_backlog.tail = NULL; in __release_sock()
2899 spin_unlock_bh(&sk->sk_lock.slock); in __release_sock()
2906 sk_backlog_rcv(sk, skb); in __release_sock()
2913 spin_lock_bh(&sk->sk_lock.slock); in __release_sock()
2920 sk->sk_backlog.len = 0; in __release_sock()
2923 void __sk_flush_backlog(struct sock *sk) in __sk_flush_backlog() argument
2925 spin_lock_bh(&sk->sk_lock.slock); in __sk_flush_backlog()
2926 __release_sock(sk); in __sk_flush_backlog()
2927 spin_unlock_bh(&sk->sk_lock.slock); in __sk_flush_backlog()
2942 int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb) in sk_wait_data() argument
2947 add_wait_queue(sk_sleep(sk), &wait); in sk_wait_data()
2948 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); in sk_wait_data()
2949 rc = sk_wait_event(sk, timeo, skb_peek_tail(&sk->sk_receive_queue) != skb, &wait); in sk_wait_data()
2950 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); in sk_wait_data()
2951 remove_wait_queue(sk_sleep(sk), &wait); in sk_wait_data()
2965 int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind) in __sk_mem_raise_allocated() argument
2967 bool memcg_charge = mem_cgroup_sockets_enabled && sk->sk_memcg; in __sk_mem_raise_allocated()
2968 struct proto *prot = sk->sk_prot; in __sk_mem_raise_allocated()
2972 sk_memory_allocated_add(sk, amt); in __sk_mem_raise_allocated()
2973 allocated = sk_memory_allocated(sk); in __sk_mem_raise_allocated()
2975 !(charged = mem_cgroup_charge_skmem(sk->sk_memcg, amt, in __sk_mem_raise_allocated()
2980 if (allocated <= sk_prot_mem_limits(sk, 0)) { in __sk_mem_raise_allocated()
2981 sk_leave_memory_pressure(sk); in __sk_mem_raise_allocated()
2986 if (allocated > sk_prot_mem_limits(sk, 1)) in __sk_mem_raise_allocated()
2987 sk_enter_memory_pressure(sk); in __sk_mem_raise_allocated()
2990 if (allocated > sk_prot_mem_limits(sk, 2)) in __sk_mem_raise_allocated()
2995 if (atomic_read(&sk->sk_rmem_alloc) < sk_get_rmem0(sk, prot)) in __sk_mem_raise_allocated()
2999 int wmem0 = sk_get_wmem0(sk, prot); in __sk_mem_raise_allocated()
3001 if (sk->sk_type == SOCK_STREAM) { in __sk_mem_raise_allocated()
3002 if (sk->sk_wmem_queued < wmem0) in __sk_mem_raise_allocated()
3004 } else if (refcount_read(&sk->sk_wmem_alloc) < wmem0) { in __sk_mem_raise_allocated()
3009 if (sk_has_memory_pressure(sk)) { in __sk_mem_raise_allocated()
3012 if (!sk_under_memory_pressure(sk)) in __sk_mem_raise_allocated()
3014 alloc = sk_sockets_allocated_read_positive(sk); in __sk_mem_raise_allocated()
3015 if (sk_prot_mem_limits(sk, 2) > alloc * in __sk_mem_raise_allocated()
3016 sk_mem_pages(sk->sk_wmem_queued + in __sk_mem_raise_allocated()
3017 atomic_read(&sk->sk_rmem_alloc) + in __sk_mem_raise_allocated()
3018 sk->sk_forward_alloc)) in __sk_mem_raise_allocated()
3024 if (kind == SK_MEM_SEND && sk->sk_type == SOCK_STREAM) { in __sk_mem_raise_allocated()
3025 sk_stream_moderate_sndbuf(sk); in __sk_mem_raise_allocated()
3030 if (sk->sk_wmem_queued + size >= sk->sk_sndbuf) { in __sk_mem_raise_allocated()
3033 mem_cgroup_charge_skmem(sk->sk_memcg, amt, in __sk_mem_raise_allocated()
3041 trace_sock_exceed_buf_limit(sk, prot, allocated, kind); in __sk_mem_raise_allocated()
3043 sk_memory_allocated_sub(sk, amt); in __sk_mem_raise_allocated()
3046 mem_cgroup_uncharge_skmem(sk->sk_memcg, amt); in __sk_mem_raise_allocated()
3061 int __sk_mem_schedule(struct sock *sk, int size, int kind) in __sk_mem_schedule() argument
3065 sk->sk_forward_alloc += amt << PAGE_SHIFT; in __sk_mem_schedule()
3066 ret = __sk_mem_raise_allocated(sk, size, amt, kind); in __sk_mem_schedule()
3068 sk->sk_forward_alloc -= amt << PAGE_SHIFT; in __sk_mem_schedule()
3080 void __sk_mem_reduce_allocated(struct sock *sk, int amount) in __sk_mem_reduce_allocated() argument
3082 sk_memory_allocated_sub(sk, amount); in __sk_mem_reduce_allocated()
3084 if (mem_cgroup_sockets_enabled && sk->sk_memcg) in __sk_mem_reduce_allocated()
3085 mem_cgroup_uncharge_skmem(sk->sk_memcg, amount); in __sk_mem_reduce_allocated()
3087 if (sk_under_memory_pressure(sk) && in __sk_mem_reduce_allocated()
3088 (sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0))) in __sk_mem_reduce_allocated()
3089 sk_leave_memory_pressure(sk); in __sk_mem_reduce_allocated()
3097 void __sk_mem_reclaim(struct sock *sk, int amount) in __sk_mem_reclaim() argument
3100 sk->sk_forward_alloc -= amount << PAGE_SHIFT; in __sk_mem_reclaim()
3101 __sk_mem_reduce_allocated(sk, amount); in __sk_mem_reclaim()
3105 int sk_set_peek_off(struct sock *sk, int val) in sk_set_peek_off() argument
3107 sk->sk_peek_off = val; in sk_set_peek_off()
3176 int sock_no_sendmsg_locked(struct sock *sk, struct msghdr *m, size_t len) in sock_no_sendmsg_locked() argument
3206 sock_update_netprioidx(&sock->sk->sk_cgrp_data); in __receive_sock()
3207 sock_update_classid(&sock->sk->sk_cgrp_data); in __receive_sock()
3225 ssize_t sock_no_sendpage_locked(struct sock *sk, struct page *page, in sock_no_sendpage_locked() argument
3235 res = kernel_sendmsg_locked(sk, &msg, &iov, 1, size); in sock_no_sendpage_locked()
3245 static void sock_def_wakeup(struct sock *sk) in sock_def_wakeup() argument
3250 wq = rcu_dereference(sk->sk_wq); in sock_def_wakeup()
3256 static void sock_def_error_report(struct sock *sk) in sock_def_error_report() argument
3261 wq = rcu_dereference(sk->sk_wq); in sock_def_error_report()
3264 sk_wake_async(sk, SOCK_WAKE_IO, POLL_ERR); in sock_def_error_report()
3268 void sock_def_readable(struct sock *sk) in sock_def_readable() argument
3273 wq = rcu_dereference(sk->sk_wq); in sock_def_readable()
3277 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); in sock_def_readable()
3281 static void sock_def_write_space(struct sock *sk) in sock_def_write_space() argument
3290 if (sock_writeable(sk)) { in sock_def_write_space()
3291 wq = rcu_dereference(sk->sk_wq); in sock_def_write_space()
3297 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); in sock_def_write_space()
3307 static void sock_def_write_space_wfree(struct sock *sk) in sock_def_write_space_wfree() argument
3312 if (sock_writeable(sk)) { in sock_def_write_space_wfree()
3313 struct socket_wq *wq = rcu_dereference(sk->sk_wq); in sock_def_write_space_wfree()
3322 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); in sock_def_write_space_wfree()
3326 static void sock_def_destruct(struct sock *sk) in sock_def_destruct() argument
3330 void sk_send_sigurg(struct sock *sk) in sk_send_sigurg() argument
3332 if (sk->sk_socket && sk->sk_socket->file) in sk_send_sigurg()
3333 if (send_sigurg(&sk->sk_socket->file->f_owner)) in sk_send_sigurg()
3334 sk_wake_async(sk, SOCK_WAKE_URG, POLL_PRI); in sk_send_sigurg()
3338 void sk_reset_timer(struct sock *sk, struct timer_list* timer, in sk_reset_timer() argument
3342 sock_hold(sk); in sk_reset_timer()
3346 void sk_stop_timer(struct sock *sk, struct timer_list* timer) in sk_stop_timer() argument
3349 __sock_put(sk); in sk_stop_timer()
3353 void sk_stop_timer_sync(struct sock *sk, struct timer_list *timer) in sk_stop_timer_sync() argument
3356 __sock_put(sk); in sk_stop_timer_sync()
3360 void sock_init_data(struct socket *sock, struct sock *sk) in sock_init_data() argument
3362 sk_init_common(sk); in sock_init_data()
3363 sk->sk_send_head = NULL; in sock_init_data()
3365 timer_setup(&sk->sk_timer, NULL, 0); in sock_init_data()
3367 sk->sk_allocation = GFP_KERNEL; in sock_init_data()
3368 sk->sk_rcvbuf = READ_ONCE(sysctl_rmem_default); in sock_init_data()
3369 sk->sk_sndbuf = READ_ONCE(sysctl_wmem_default); in sock_init_data()
3370 sk->sk_state = TCP_CLOSE; in sock_init_data()
3371 sk_set_socket(sk, sock); in sock_init_data()
3373 sock_set_flag(sk, SOCK_ZAPPED); in sock_init_data()
3376 sk->sk_type = sock->type; in sock_init_data()
3377 RCU_INIT_POINTER(sk->sk_wq, &sock->wq); in sock_init_data()
3378 sock->sk = sk; in sock_init_data()
3379 sk->sk_uid = SOCK_INODE(sock)->i_uid; in sock_init_data()
3381 RCU_INIT_POINTER(sk->sk_wq, NULL); in sock_init_data()
3382 sk->sk_uid = make_kuid(sock_net(sk)->user_ns, 0); in sock_init_data()
3385 rwlock_init(&sk->sk_callback_lock); in sock_init_data()
3386 if (sk->sk_kern_sock) in sock_init_data()
3388 &sk->sk_callback_lock, in sock_init_data()
3389 af_kern_callback_keys + sk->sk_family, in sock_init_data()
3390 af_family_kern_clock_key_strings[sk->sk_family]); in sock_init_data()
3393 &sk->sk_callback_lock, in sock_init_data()
3394 af_callback_keys + sk->sk_family, in sock_init_data()
3395 af_family_clock_key_strings[sk->sk_family]); in sock_init_data()
3397 sk->sk_state_change = sock_def_wakeup; in sock_init_data()
3398 sk->sk_data_ready = sock_def_readable; in sock_init_data()
3399 sk->sk_write_space = sock_def_write_space; in sock_init_data()
3400 sk->sk_error_report = sock_def_error_report; in sock_init_data()
3401 sk->sk_destruct = sock_def_destruct; in sock_init_data()
3403 sk->sk_frag.page = NULL; in sock_init_data()
3404 sk->sk_frag.offset = 0; in sock_init_data()
3405 sk->sk_peek_off = -1; in sock_init_data()
3407 sk->sk_peer_pid = NULL; in sock_init_data()
3408 sk->sk_peer_cred = NULL; in sock_init_data()
3409 spin_lock_init(&sk->sk_peer_lock); in sock_init_data()
3411 sk->sk_write_pending = 0; in sock_init_data()
3412 sk->sk_rcvlowat = 1; in sock_init_data()
3413 sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; in sock_init_data()
3414 sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; in sock_init_data()
3416 sk->sk_stamp = SK_DEFAULT_STAMP; in sock_init_data()
3418 seqlock_init(&sk->sk_stamp_seq); in sock_init_data()
3420 atomic_set(&sk->sk_zckey, 0); in sock_init_data()
3423 sk->sk_napi_id = 0; in sock_init_data()
3424 sk->sk_ll_usec = READ_ONCE(sysctl_net_busy_read); in sock_init_data()
3427 sk->sk_max_pacing_rate = ~0UL; in sock_init_data()
3428 sk->sk_pacing_rate = ~0UL; in sock_init_data()
3429 WRITE_ONCE(sk->sk_pacing_shift, 10); in sock_init_data()
3430 sk->sk_incoming_cpu = -1; in sock_init_data()
3431 sk->sk_txrehash = SOCK_TXREHASH_DEFAULT; in sock_init_data()
3433 sk_rx_queue_clear(sk); in sock_init_data()
3439 refcount_set(&sk->sk_refcnt, 1); in sock_init_data()
3440 atomic_set(&sk->sk_drops, 0); in sock_init_data()
3444 void lock_sock_nested(struct sock *sk, int subclass) in lock_sock_nested() argument
3447 mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_); in lock_sock_nested()
3450 spin_lock_bh(&sk->sk_lock.slock); in lock_sock_nested()
3451 if (sock_owned_by_user_nocheck(sk)) in lock_sock_nested()
3452 __lock_sock(sk); in lock_sock_nested()
3453 sk->sk_lock.owned = 1; in lock_sock_nested()
3454 spin_unlock_bh(&sk->sk_lock.slock); in lock_sock_nested()
3458 void release_sock(struct sock *sk) in release_sock() argument
3460 spin_lock_bh(&sk->sk_lock.slock); in release_sock()
3461 if (sk->sk_backlog.tail) in release_sock()
3462 __release_sock(sk); in release_sock()
3467 if (sk->sk_prot->release_cb) in release_sock()
3468 sk->sk_prot->release_cb(sk); in release_sock()
3470 sock_release_ownership(sk); in release_sock()
3471 if (waitqueue_active(&sk->sk_lock.wq)) in release_sock()
3472 wake_up(&sk->sk_lock.wq); in release_sock()
3473 spin_unlock_bh(&sk->sk_lock.slock); in release_sock()
3477 bool __lock_sock_fast(struct sock *sk) __acquires(&sk->sk_lock.slock) in __lock_sock_fast() argument
3480 spin_lock_bh(&sk->sk_lock.slock); in __lock_sock_fast()
3482 if (!sock_owned_by_user_nocheck(sk)) { in __lock_sock_fast()
3501 __lock_sock(sk); in __lock_sock_fast()
3502 sk->sk_lock.owned = 1; in __lock_sock_fast()
3503 __acquire(&sk->sk_lock.slock); in __lock_sock_fast()
3504 spin_unlock_bh(&sk->sk_lock.slock); in __lock_sock_fast()
3512 struct sock *sk = sock->sk; in sock_gettstamp() local
3515 sock_enable_timestamp(sk, SOCK_TIMESTAMP); in sock_gettstamp()
3516 ts = ktime_to_timespec64(sock_read_timestamp(sk)); in sock_gettstamp()
3521 sock_write_timestamp(sk, kt); in sock_gettstamp()
3548 void sock_enable_timestamp(struct sock *sk, enum sock_flags flag) in sock_enable_timestamp() argument
3550 if (!sock_flag(sk, flag)) { in sock_enable_timestamp()
3551 unsigned long previous_flags = sk->sk_flags; in sock_enable_timestamp()
3553 sock_set_flag(sk, flag); in sock_enable_timestamp()
3559 if (sock_needs_netstamp(sk) && in sock_enable_timestamp()
3565 int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len, in sock_recv_errqueue() argument
3573 skb = sock_dequeue_err_skb(sk); in sock_recv_errqueue()
3586 sock_recv_timestamp(msg, sk, skb); in sock_recv_errqueue()
3611 struct sock *sk = sock->sk; in sock_common_getsockopt() local
3614 return READ_ONCE(sk->sk_prot)->getsockopt(sk, level, optname, optval, optlen); in sock_common_getsockopt()
3621 struct sock *sk = sock->sk; in sock_common_recvmsg() local
3625 err = sk->sk_prot->recvmsg(sk, msg, size, flags, &addr_len); in sock_common_recvmsg()
3638 struct sock *sk = sock->sk; in sock_common_setsockopt() local
3641 return READ_ONCE(sk->sk_prot)->setsockopt(sk, level, optname, optval, optlen); in sock_common_setsockopt()
3645 void sk_common_release(struct sock *sk) in sk_common_release() argument
3647 if (sk->sk_prot->destroy) in sk_common_release()
3648 sk->sk_prot->destroy(sk); in sk_common_release()
3658 sk->sk_prot->unhash(sk); in sk_common_release()
3672 sock_orphan(sk); in sk_common_release()
3674 xfrm_sk_free_policy(sk); in sk_common_release()
3676 sk_refcnt_debug_release(sk); in sk_common_release()
3678 sock_put(sk); in sk_common_release()
3682 void sk_get_meminfo(const struct sock *sk, u32 *mem) in sk_get_meminfo() argument
3686 mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk); in sk_get_meminfo()
3687 mem[SK_MEMINFO_RCVBUF] = READ_ONCE(sk->sk_rcvbuf); in sk_get_meminfo()
3688 mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk); in sk_get_meminfo()
3689 mem[SK_MEMINFO_SNDBUF] = READ_ONCE(sk->sk_sndbuf); in sk_get_meminfo()
3690 mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc; in sk_get_meminfo()
3691 mem[SK_MEMINFO_WMEM_QUEUED] = READ_ONCE(sk->sk_wmem_queued); in sk_get_meminfo()
3692 mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc); in sk_get_meminfo()
3693 mem[SK_MEMINFO_BACKLOG] = READ_ONCE(sk->sk_backlog.len); in sk_get_meminfo()
3694 mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops); in sk_get_meminfo()
4074 struct sock *sk = p; in sk_busy_loop_end() local
4076 return !skb_queue_empty_lockless(&sk->sk_receive_queue) || in sk_busy_loop_end()
4077 sk_busy_loop_timeout(sk, start_time); in sk_busy_loop_end()
4082 int sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len) in sock_bind_add() argument
4084 if (!sk->sk_prot->bind_add) in sock_bind_add()
4086 return sk->sk_prot->bind_add(sk, addr, addr_len); in sock_bind_add()